简体   繁体   中英

Http Error 405/500 in Python/Mechanize (using mechanize auto login one website)

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

And here is my code:

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()

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

sometimes it will throws Http Error 405:

HTTP Error 405: Method Not Allowed

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


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()

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM