python - 尝试使用 mechanize 登录 quora

标签 python http mechanize quora

我正在尝试使用 mechanize 模块登录 quora.com。

这是我的代码:

#!/usr/bin/env python

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

我知道 HTTP 500 意味着服务器端出现问题,但我不知道它是什么,以及如何调试该问题。

最佳答案

我敢打赌mechanize无法处理提交这个特定的表单。使用浏览器开发人员工具,您可以看到此表单是通过 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:{}

仅供引用,如果您通过 br.set_debug_http(True) 打开调试,您会看到您的脚本在后台向主页 http://www 发出 POST 请求.quora.com 具有以下参数:

email=<email>
allow_passwordless=allow_passwordless
password=<password>

如您所见:错误的 URL 和不同的参数。

我会使用selenium对于这个特殊情况。

希望有帮助。

关于python - 尝试使用 mechanize 登录 quora,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17471579/

相关文章:

python - 亚马逊网络服务 : -bash: activate: No such file or directory

python - 使用 Python 登录 AJAX 表单

Python cx-freeze 快捷方式图标

python - 使用 futures.ProcessPoolExecutor 但不使用带有 PRAW 包装器的 futures.ThreadPoolExecutor 时的递归最大错误

python - padding_idx 在 nn.embeddings() 中做什么

python - 在使用 DHTML、AJAX 等的网站上使用 Python Mechanize ?

Python Mechanize : submit button will not work no attribute 'click'

http - 注入(inject)的记录器在 angular2 的自定义 Http 中未定义

http - 确定 Web 应用程序中的同步与异步

http - 将无标题项添加到集合中的 RESTful 方式是什么?