我正在尝试使用mechanize模块登录quora。以下是我正在使用的代码:
import mechanize
import cookielib
br = mechanize.Browser() # create a browser object
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_headers = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)')]
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
res = br.open('http://www.quora.com')
br.select_form(nr = 0)
br.form['email'] = 'uuuu'
br.form['password'] = 'pppp'
res = br.submit()
print res.read()
我收到以下错误:
Traceback (most recent call last):
File "mech.py", line 29, in <module>
res = br.submit()
File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 541, in submit
return self.open(self.click(*args, **kwds))
File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open
return self._mech_open(url, data, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open
raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 500: Internal Server Error
同一个问题的答案之一就是因为这个特殊的表单是通过javascript XHR POST请求提交给https://www.quora.com/webnode2/server_call_POST?instart的,其中请求参数如下所示:
json:{"args":[],"kwargs":{"email":"<email>","password":"<password>","passwordless":1}}
formkey:62c4f0d88246bfd81b27cf0dca410d75
window_id:dep4-4597603286175583039
_lm_transaction_id:0.4317954108119011
_lm_window_id:dep4-4597603286175583039
__vcon_json:["hmac","t1cKg1QhQsYPCA"]
__vcon_method:do_login
js_init:{}
有没有任何模块可以解决这个问题?
P.S。我使用了硒,它起作用但速度非常慢。