尝试使用mechanize登录quora [英] Trying to login to quora using mechanize

查看:199
本文介绍了尝试使用mechanize登录quora的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use mechanize module to login to 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意味着服务器端出现了问题,但我不知道它是什么,以及如何调试问题。

I know that HTTP 500 means there is something wrong server-side, but I have no idea what it is, and how I can debug the issue.

推荐答案

我打赌 mechanize 无法处理提交此特定表单。使用浏览器开发人员工具,您可以看到此表单是通过javascript XHR POST请求提交到 https://www.quora.com/webnode2/server_call_POST?__instart__ 其中请求参数的外观喜欢:

I bet mechanize can't handle submitting this particular form. Using browser developer tools you can see that this form is submitted via javascript XHR POST request to https://www.quora.com/webnode2/server_call_POST?__instart__ where request parameters looks like:

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.quora.com 发出POST请求:

FYI, if you turn on debugging via br.set_debug_http(True) you'll see that your script behind the scenes makes a POST request to the main page http://www.quora.com with the following parameters:

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

正如您所见:错误的网址和不同的参数。

As you can see: wrong URL and different parameters.

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

I'd use selenium for this particular case.

希望有所帮助。

这篇关于尝试使用mechanize登录quora的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆