Automate Website Logins with Python Requests

Feb 3, 2024 ยท 2 min read

Logging into websites is a common task when automating workflows with Python. The requests module makes this simple with its built-in request methods.

In this guide, I'll demonstrate how to login to a website by POSTing login credentials with

Construct the Login Request

We need to inspect the website's login form to understand how to structure the request. Most login forms submit user credentials via POST to an endpoint like /login or /auth.

Here's an example login form:

<form action="/login" method="post">
  <input type="text" name="username">
  <input type="password" name="password">
  <button type="submit">Login</button>

This form submits a POST request to /login with the username and password fields.

We can replicate that in Python:

import requests

url = ""
payload = {"username": "myuser", "password": "mypass"}
response =, data=payload)

This will POST the payload dict containing the credentials to the URL.

Handling Login Responses

We need to check the response status code to see if the login succeeded. A 200 OK means it worked. Other codes like 401 Unauthorized indicate a failed login.

if response.status_code == 200:
    print("Login succeeded!")
    print("Error logging in.") 

The website may also use cookies or tokens to maintain logged in state, which requests will automatically handle.


The requests module makes automating logins easy. By inspecting the form fields and URLs, we can replicate the login process in Python. Properly handling the different response codes ensures our scripts know when the login worked or failed.

This opens up many possibilities like creating login bots, automating admin dashboards, and more. Give it a try on your websites!

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you

Try ProxiesAPI for free

curl ""

<!doctype html>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />


Don't leave just yet!

Enter your email below to claim your free API key: