How to Login to a Website with Python

How can I login to a website with Python?

Maybe you want to use twill. It's quite easy to use and should be able to do what you want.

It will look like the following:

from twill.commands import *

fv("1", "email-email", "")
fv("1", "password-clear", "testpass")


You can use showforms() to list all forms once you used go… to browse to the site you want to login. Just try it from the python interpreter.

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.

How to login to a website by using Python

Perhaps it is expecting User not username:

url = 'http://12.345.67.891:8000/login'
values = {'User': 'abcdefg',
'password': 'apple'}

r =, data=values)

Login to website using python requests

As said above, you should send values of all fields of form. Those can be find in the Web inspector of browser. This form send 2 addition hidden values:

url = ""
data = {'userName':'xxxxxxxxx','password':'yyyyyyyyy','challenge':'zzzzzzzzz','hash':''}
# note that in email have encoded '@' like

session = requests.Session()
r =, headers=headers, data=data)

Also, many sites have protection from a bot like hidden form fields, js, send encoded values, etc. As variants you could:

1) Use a cookies from manual login:

url = ""
headers = {'user-agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36"}
cookies = {'PHPSESSID':'zzzzzzzzzzzzzzz', 'loggedIn':'yes'}

s = requests.Session()
r =, headers=headers, cookies=cookies)

2) Use module Selenium:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = ""
driver = webdriver.Firefox()

u = driver.find_element_by_name('userName')
p = driver.find_element_by_name('password')

How to Login to a website and hit any url and get the response by using Python

You will probably need to look into how the authentication mechanism works in HTTP. It's most likely that your server is returning either a cookie or some sort of other identification header. Cookies are easiest because the browser will (to a first approximation) automatically return the cookies it gets form a server when making further requests. Your existing code isn't doing that.

Since you are using the requests library you should look at the answer to this question, which might shed some light on the problem.

Related Topics

Leave a reply