Python/机械化中的Http错误405/500(使用机械化自动登录一个网站) [英] Http Error 405/500 in Python/Mechanize (using mechanize auto login one website)

查看:152
本文介绍了Python/机械化中的Http错误405/500(使用机械化自动登录一个网站)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要使用python/mechanize自动登录的网站地址:

Here is the website's address on which i want to using python/mechanize to auto login:

http://www.autohome.com.cn/tools/hometoplogin.html

这是我的代码:

br = Browser()
login_url = 'http://www.autohome.com.cn/tools/hometoplogin.html'
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.open(login_url)
br.select_form(nr = 0)
br['name'] = username
br['pwd'] = password
print br.submit().read()

但是代码抛出Http错误500:

However the codes throws Http Error 500:

File "build\bdist.win32\egg\mechanize\_mechanize.py", line 541, in submit
File "build\bdist.win32\egg\mechanize\_mechanize.py", line 203, in open
File "build\bdist.win32\egg\mechanize\_mechanize.py", line 255, in _mech_open
mechanize._response.httperror_seek_wrapper: HTTP Error 500: Internal Server Error

有时会抛出Http错误405:

sometimes it will throws Http Error 405:

HTTP Error 405: Method Not Allowed

现在我很困惑...请帮助...

Now i am so confused...please help...

现在我已经修复了如下所示的python代码

now i have fixed my python code , shown below

username = 'staryin'
password = 'zxy1985'
login_url = 'http://www.autohome.com.cn/tools/hometoplogin.html'
br = Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
br.open(login_url)
br.select_form(nr = 0)
br['name'] = username
br['pwd'] = password
br.form.action = login_url
br.submit().read()

它还会引发如下所示的http错误405

it also throws http error 405 shown in below

File "build\bdist.win32\egg\mechanize\_mechanize.py", line 541, in submit
File "build\bdist.win32\egg\mechanize\_mechanize.py", line 203, in open
File "build\bdist.win32\egg\mechanize\_mechanize.py", line 255, in _mech_open
mechanize._response.httperror_seek_wrapper: HTTP Error 405: Method Not Allowed

谁能帮助我解决这个问题?非常感谢.

who can help me to solve this problem?thanks a lot.

推荐答案

如果您可以使用浏览器打开此页面,则该网站很可能不允许像您这样的机器人或程序访问或登录.如Kimvais所说,必须通过设置您的User-Agent标头来伪造您的身份.

If you can open this page with a browser, chances are that the website does not allow bots or programs like yours to access or log in. In that case you must fake your identity by setting your User-Agent header, as Kimvais said.

这篇关于Python/机械化中的Http错误405/500(使用机械化自动登录一个网站)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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