我试图使用在Kent's Korner中读取的代码进行基于表单的身份验证。至少我被告知我正在阅读的网站是基于表单的认证。在
但我似乎无法通过登录页面。我使用的代码是
Import urllib, urllib2, cookielib, string
# configure an opener that will handle cookies
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
# use the opener to POST to the login form and the protected page
params = urllib.urlencode(dict(username='user', password='stuff'))
f = opener.open('http://www.hammernutrition.com/forums/memberlist.php?mode=viewprofile&u=1323', params)
data = f.read()
f.close()
f = opener.open('http://www.hammernutrition.com/forums/memberlist.php?mode=viewprofile&u=1323')
data = f.read()
f.close()
您可以在Python中模拟web浏览器,而不用使用mechanize太多的资源 (Debian/Ubuntu包名为
python-mechanize
)。它处理cookies和提交表单,就像web浏览器一样,一个很好的例子是Python Dropbox Uploader脚本,您可以根据需要对其进行转换。在相关问题 更多 >
编程相关推荐