Fix Python – How to “log in” to a website using Python’s Requests module?


Asked By – Marcus Johnson

I am trying to post a request to log in to a website using the Requests module in Python but its not really working. I’m new to this…so I can’t figure out if I should make my Username and Password cookies or some type of HTTP authorization thing I found (??).

from pyquery import PyQuery
import requests

url = ''

So now, I think I’m supposed to use “post” and cookies….

ck = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}

r =, cookies=ck)

content = r.text

q = PyQuery(content)

title = q("title").text()

print title

I have a feeling that I’m doing the cookies thing wrong…I don’t know.

If it doesn’t log in correctly, the title of the home page should come out to “” and if it does, it should be “Home Page.”

If you could maybe explain a few things about requests and cookies to me and help me out with this, I would greatly appreciate it. 😀


…It still didn’t really work yet. Okay…so this is what the home page HTML says before you log in:

</td><td><img src="">    </td>
<td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName"  size="25"></td>
<td><img src=""> </td>
<td><input  class="Data_Entry_Field_Login"  type="password" name="inUserPass"     id="inUserPass"></td>

So I think I’m doing it right, but the output is still “”

2nd EDIT:

I want to be able to stay logged in for a long time and whenever I request a page under that domain, I want the content to show up as if I were logged in.

Now we will see solution for issue: How to “log in” to a website using Python’s Requests module?


If the information you want is on the page you are directed to immediately after login…

Lets call your ck variable payload instead, like in the python-requests docs:

payload = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}
url = '', data=payload)


See below.

This question is answered By – katy lavallee

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0