使用mechanize/python验证密码失败

2024-06-25 06:44:21 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我只想说有很多类似的问题,但没有一个对我有用。你知道吗

我是一个python/scraping新手,我正在尝试使用mechanize模块登录到一个网站。但是,当我以表单形式提供正确的凭据时,它无法登录。这是我的密码:

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Chrome')]

br.open('https://www.chess.com/login')
br.select_form(nr=0)
br.form['c1'] = 'username'
br.form['loginpassword'] = 'password'

br.submit()

print br.open('http://www.chess.com/home/game_archive?member=username&page=1').read()

最后的print命令显示页面的源代码,就好像我没有登录一样。如果我通过chrome输入相同的凭证,访问最终的url,然后查看源代码,我可以看到正确的(登录的)代码,所以用户名和密码应该不是问题。有人能猜出发生了什么事吗?你知道吗


Tags: brformcomtruehttp密码wwwusername