Python: requests can't login to a website

Try doing a get on the login page first. Perhaps it's setting some cookies that it expects to be present on the post.


I know that this question was made long ago, but anyway, I'll propose a solution for those who are still having trouble with this: I recommend to check if the form you are trying to post takes some kind of hidden input, which the example of the question does. This is very frequent, and does sometimes prevent us from logging to a site if we do not notice it. So, let's suppose in the site there is a form like this:

<form method='post' id='signin-form' class='big-form'>
 <input type="hidden" id="whatever" name="foo" value="check">
 <input type="text" id="u" name="user">
 <input type="password" id="pwd" name="pass">
</form>

In that case, the variable login_data should be like this:

login_data = {
       "foo":"check",
       "user":"your username",
       "pass":"your password",           
}

Having done this, and provided the website does not check the headers, you should have no trouble logging to a website via the requests module.