Http Error 405/500 in Python/Mechanize (using mech

2019-06-25 05:51发布

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.

1条回答
可以哭但决不认输i
2楼-- · 2019-06-25 06:34

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.

查看更多
登录 后发表回答