嗨,我正在尝试使用Python和Mechanize库为浏览器游戏编写机器人程序。我陷入了身份验证步骤,因为当我尝试提交表单时,使用Python收到了400错误的请求,但使用浏览器就可以了
Python代码
from mechanize import Browser
import mechanize
browser = Browser()
browser.set_debug_http(True)
HEADERS = [('User-agent', 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Mobile Safari/537.36')]
browser.set_handle_equiv(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
browser.addheaders = HEADERS
browser.open('https://it.ogame.gameforge.com/')
browser.viewing_html()
browser.select_form(name='loginForm')
browser['login'] = 'email'
browser['pass'] = 'password'
response = browser.submit()
print(response.read())
请求调试响应
send: 'POST /main/login HTTP/1.1\r\nAccept-Encoding: identity\r\nContent-
Length: 30\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Mobile Safari/537.36\r\nHost: it.ogame.gameforge.com:443\r\nCookie: SID=rkq1jb7h8prj8hstsmgv3tkgv5\r\nReferer: https://it.ogame.gameforge.com/\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nkid=&login=email&pass=password'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Mon, 30 Jul 2018 21:39:52 GMT
header: Server: Apache
header: Vary: Accept-Encoding
header: Content-Length: 214
header: Connection: close
header: Content-Type: text/html; charset=UTF-8
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>400 Bad Request</title>
</head>
<body>
<h1>Bad Request</h1>
<p>The request could not be understood by the server.</p>
</body>
</html>
网站HTML(仅表单的一部分)
<form id="loginForm" name="loginForm" method="post" action="https://it.ogame.gameforge.com:443/main/login">
<input type="hidden" name="kid" value="">
<div class="input-wrap">
<label style="display: inline-block; width: auto" for="usernameLogin">Indirizzo E-mail:</label>
<a style="" tabindex="4" href="https://lobby.ogame.gameforge.com/retrieveAccountEmail?language=it" id="1emailLost" target="_blank" title="Indirizzo e-mail dimenticato?">(Indirizzo e-mail dimenticato?)</a>
<div class="black-border">
<input class="js_userName" type="text" tabindex="1" onkeydown="hideLoginErrorBox();" id="usernameLogin" name="login" value="">
<div class="usernameLoginformError parentFormloginForm formError" style="opacity: 0.87; position: absolute; top: -2px; left: 385px; right: initial; margin-top: 0px;">
<div class="formErrorContent">Il tuo indirizzo e-mail non è valido oppure la tua password non è corretta.</div>
</div>
</div>
<div id="usernameLogin_dialog" class="right">
</div>
</div>
<div class="input-wrap">
<label style="display: inline-block; width: auto" for="passwordLogin">Password:</label>
<a style="" tabindex="4" href="https://lobby.ogame.gameforge.com/passwordReset?language=it" id="1pwLost" target="_blank" title="Password dimenticata?">(Password dimenticata?)</a>
<div class="black-border">
<input type="password" tabindex="2" onkeydown="hideLoginErrorBox();" id="passwordLogin" name="pass">
</div>
</div>
<div class="input-wrap">
<div id="autoLoginElement">
<input type="checkbox" id="autoLogin" title="Login automatico">
Login automatico
</div>
<p id="TermsAndConditionsAcceptWithLogin">
Effettuando il login accetto le <a class="" href="https://agbserver.gameforge.com/rewrite.php locale=it&type=switch&product=ogame" target="_blank" title="T&C">T&C</a>.
</p>
</div>
<input type="submit" id="loginSubmit" value="Login" tabindex="3">
答案 0 :(得分:0)
乍一看似乎是在寻找错误的编写方法...当您像这样在python代码中设置值时
browser['login'] = 'email'
browser['pass'] = 'password'
您应该将其更改为
browser.form['login'] = 'email'
browser.form['pass'] = 'password'
根据库文档:https://mechanize.readthedocs.io/en/latest/forms_api.html#mechanize.HTMLForm.set_value