Python Mechanize - 为什么我不能浏览此链接?

时间:2013-08-11 11:51:42

标签: python mechanize

我正在尝试使用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()

1 个答案:

答案 0 :(得分:0)

我建议你看一下这篇文章Download file from web in Python 3,讨论如何从链接下载文件。