我无法使用 Python 中的请求库发送 POST 数据以访问我的帐户。结果soup
与未发送 POST 相同 这是我一直在使用并在不同站点上工作的代码:
def get_data(final_url):
payload = {'session[email]':'myemail@myemail.com','session[password]':'mypwd','session[remember_me]':'0','commit':'Sign in'}
with requests.session() as ses:
log_soup = BeautifulSoup(ses.get('https://www.login-page.com/login').text)
payload['utf8'] = log_soup.findAll('input',attrs={'name':'utf8'})[0].get('value')
payload['authenticity_token'] = log_soup.findAll('input',attrs={'name':'authenticity_token'})[0].get('value')
ses.post('https://www.login-page.com/login',data=payload)
req = ses.get(final_url)
soup = BeautifulSoup(req.text)
return soup
<form accept-charset="UTF-8" action="/sign_in" class="main_form" id="new_session" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"/><input name="authenticity_token" type="hidden" value="l5+2s90FtEUsS3GHlr2tAktcxSW8jqgXx3mXEwIlAzE="/></div><div class="error_message">
</div>
<dl>
<dt><label for="session_email">Email</label></dt>
<dd>
<div class="input_border"><input autofocus="autofocus" class="text required email" id="session_email" name="session[email]" placeholder="Email" size="30" type="email"/></div>
</dd>
</dl>
<dl>
<dt><label for="session_password">Password</label></dt>
<dd>
<div class="input_border"><input class="text required" id="session_password" name="session[password]" placeholder="Password" size="30" type="password"/></div>
</dd>
</dl>
<div class="checkbox_field"><input name="session[remember_me]" type="hidden" value="0"/><input id="session_remember_me" name="session[remember_me]" type="checkbox" value="1"/><label for="session_remember_me">Remember me</label></div>
<table class="buttons">
<tr>
<td><input class="button public" data-disable-with="Please wait..." name="commit" type="submit" value="Sign in"/></td>
<td class="forgot_password"><a href="/passwords/new">Forgot your password?</a></td>
</tr>
</table>
</form>
手动登录后得到的POST数据如下:
utf8:✓
authenticity_token:l5+2s90FtEUsS3GHlr2tAktcxSW8jqgXx3mXEwIlAzE=
session[email]:myemail@myemail.com
session[password]:mypwd
session[remember_me]:0
非常感谢您的帮助!