简体   繁体   中英

Python Mechanize - Why can't I navigate this link?

I am trying to download a file using mechanize from http://www.filehippo.com/download_google_chrome/ so I can learn to use Mechanize with Python.

My code returns the following error which I assume means it can't find the link? What am I doing wrong?

Error

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

My Code

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()

我建议您看这篇文章了解如何从Python 3中从Web下载文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM