使用Python中的Requests库发送POST数据

时间:2013-05-10 18:41:09

标签: python html post beautifulsoup python-requests

我无法使用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="&#x2713;"/><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

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

您使用的是哪种版本的请求?如果它介于0.14.x和1.2.0之间,则必须升级到1.2.0。会话中的Cookie将无法在重定向上正确设置,而1.2.0会修复此问题。

另一个问题是,您应该检查来自ses.post(url, data=payload)的回复。你肯定应该检查你是否收到了正确的状态代码(在这种情况下,因为你期望重定向)是301或302的。

假设上述所有检查结果,您的网站可能正在进行一些时髦的User-Agent分析,您可能还想检查浏览器用户代理字符串,例如,

ses.headers['User-Agent'] = '...'
response = ses.post(url, data=payload)
final_response = ses.get(final_url)