PINGDOM_CHECK
ArticleTutorial / How-toProduct Update

How To Fill Login Forms Automatically

We often have to write spiders that need to fill login forms to sites. Our customers provide us with the site, username and password, and we do the rest.

Pablo Hoffman

3 min read ·

How to fill login forms automatically

We often have to write spiders that need to login to sites, in order to scrape data from them. Our customers provide us with the site, username and password, and we do the rest.

The classic way to approach this problem is:

  1. launch a browser, go to site and search for the login page
  2. inspect the source code of the page to find out:
    1. which one is the login form (a page can have many forms, but usually one of them is the login form)
    2. which are the field names used for username and password (these could vary a lot)
    3. if there are other fields that must be submitted (like an authentication token)
  3. write the Scrapy spider to replicate the form submission using FormRequest (here is an example)

Being fans of automation, we figured we could write some code to automate point 2 (which is actually the most time-consuming) and the result is loginform, a library to automatically fill login forms given the login page, username and password.

Here is the code of a simple spider that would use loginform to login to sites automatically:

Plain text

Copy to clipboard

Open code in new window

EnlighterJS 3 Syntax Highlighter

from scrapy.spider import BaseSpider

from scrapy.http import FormRequest

from loginform import fill_login_form

class LoginSpider(BaseSpider):

start_urls = ["http://github.com/login"\]

login_user = "foo"

login_pass = "bar"

def parse(self, response):

args, url, method = fill_login_form(response.url, response.body, self.login_user, self.login_pass)

return FormRequest(url, method=method, formdata=args, callback=self.after_login)

def after_login(self, response):

# you are logged in here

from scrapy.spider import BaseSpider from scrapy.http import FormRequest from loginform import fill_login_form class LoginSpider(BaseSpider): start_urls = ["http://github.com/login"\] login_user = "foo" login_pass = "bar" def parse(self, response): args, url, method = fill_login_form(response.url, response.body, self.login_user, self.login_pass) return FormRequest(url, method=method, formdata=args, callback=self.after_login) def after_login(self, response): # you are logged in here

1from scrapy.spider import BaseSpider
2from scrapy.http import FormRequest
3from loginform import fill\_login\_form
4
5class LoginSpider(BaseSpider):
6
7    start\_urls = \["http://github.com/login"\]
8    login\_user = "foo"
9    login\_pass = "bar"
10
11    def parse(self, response):
12        args, url, method = fill\_login\_form(response.url, response.body, self.login\_user, self.login\_pass)
13        return FormRequest(url, method=method, formdata=args, callback=self.after\_login)
14
15    def after\_login(self, response):
16        # you are logged in here
Copy

In addition to being open source, loginform code is very simple and easy to hack (check the README on Github for more details). It also contains a collection of HTML samples to keep the library well-tested, and a convenient tool to manage them. Even with the simple code so far, we have seen accuracy rates of 95% in our tests. We encourage everyone with similar needs to give it a try, provide feedback and contribute patches.

We contributed this project to the Scrapy group to better encourage community adoption and contributions. Like w3lib and scrapely, loginform is completely decoupled and not dependent on the Scrapy crawling framework.

Try Zyte API

Build your first scraper in minutes

Free trial, no credit card. From a single request to production in an afternoon.

Get started

Continue reading

Web scraping finally has a home in the IDE
Product Update

Web scraping finally has a home in the IDE

Discover how web scraping is moving into the IDE. Learn how tools like VS Code and AI-assisted extensions are streamlining scraper development, testing, and maintenance.

Mitch Holt10 min read
Introducing Web Scraping Copilot 1.0: AI-accelerated web scraping inside VS Code
Announcement

Introducing Web Scraping Copilot 1.0: AI-accelerated web scraping inside VS Code

Discover Web Scraping Copilot 1.0, Zyte’s VS Code extension that uses AI to generate, test, and deploy production-ready Scrapy spiders faster while maintaining full developer control.

Mitch Holt10 min read
Play Before You Scrape: Explore Zyte API Settings with Playground
Product Update

Play Before You Scrape: Explore Zyte API Settings with Playground

Discover the best way to configure your scrapers using Zyte API Playground

Cleber Alexandre10 min read
Scraping food delivery data from any geo while avoiding IP-based restrictions
Announcement

Scraping food delivery data from any geo while avoiding IP-based restrictions

Geolocation technology is revolutionizing the food delivery industry, enabling platforms to access location-specific data.

Debbie Reeve-Crook3 min read
Extract localized data with Zyte API’s extended geolocation
How To

Extract localized data with Zyte API’s extended geolocation

High quality web data extracted from localized websites can be tricky and expensive. We share how we solved geolocation issues with Zyte API’s extended geolocation.

Mohsin Ali, Oleksandr Leshchynskyi6 min read
From Data to Decisions: Transforming Digital Shelf Analytics with Zyte API
Announcement

From Data to Decisions: Transforming Digital Shelf Analytics with Zyte API

Maximize DSA with Zyte API Web Scraping: Leverage Zyte API's advanced web scraping technology for your DSA needs. Extract precise, scalable data for product positioning, pricing strategies, and market trends analysis. Stay ahead with our legally compliant, flexible, and cost-effective solutions.

Debbie Reeve-Crook4 min read

The Community · Newsletter

The best of Zyte and the data web, in your inbox.

One curated edition — new articles, product updates, and the stories shaping the data web. No noise.