所以这是我第一次编程,我正在尝试使用 python/mechanize 自动登录网站。所以这是我的代码:
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)
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')]
br.add_password('http://newiso.accellion.net/w', 'USERNAME', 'PASSWORD')
br.open('http://newiso.accellion.net/w')
# Show the html title
print br.title()
# Show the response headers
print br.response().info()
# Show the available forms
for f in br.forms():
print f
br.form["g_username"] = "USERNAME"
br.form["password"] = "PASSWORD"
import urllib
opener = urllib.FancyURLopener()
print opener.open('http://USERNAME:PASSWORD@newiso.accellion.net/').read()
我不断收到这个错误:
Traceback (most recent call last):
File "/Users/dancetrina/Documents/login.py", line 45, in <module>
br.form["g_username"] = "USERNAME"
TypeError: 'NoneType' object does not support item assignment
这是否意味着我不能使用 mechanize 输入用户名/密码?还是我缺少一些可以使它起作用的东西?非常感谢您!