0

我正在尝试从http://www.filehippo.com/download_google_chrome/使用 mechanize 下载文件,这样我就可以学习将 Mechanize 与 Python 一起使用。

我的代码返回以下错误,我认为这意味着它找不到链接?我究竟做错了什么?

错误

File "C:\python27\lib\site-packages\mechanize-0.2.5-py2.7.egg\mechanize\_mechanize.py", line 620, in find_link raise LinkNotFoundError() 

我的代码

import mechanize
import cookielib

# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Want debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)

# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]


# Open some site, let's pick a random one, the first that pops in mind:
r = br.open('http://www.filehippo.com/download_google_chrome/')


# Show the available forms
for f in br.forms():
    print f

# Actually clicking the link
req = br.click_link(text='Download Latest Version')
br.open(req)
print br.response().read()
print br.geturl()
4

1 回答 1

0

我建议您查看这篇文章在 Python 3 中从 Web 下载文件,该文章讨论了如何从链接下载文件。

于 2013-08-11T12:11:49.697 回答