Web scraping with Python: the first page is easy, the thousandth is not

Fetching a page and printing its title takes five lines of Python. The work starts later: the selector that breaks after a redesign, the price that comes back wrong on one page in fifty. This is the short road from a first script to one you can leave running.

Web scraping with Python

import requests
from bs4 import BeautifulSoup
html = requests.get(url, headers={"User-Agent": "my-scraper"}).text
soup = BeautifulSoup(html, "html.parser")
print(soup.select_one("h1").get_text(strip=True))

Nederlands

Web scraping in Python, step by step

  1. Fetch the page. requests.get(url).text is enough for most pages. Send a clear User-Agent and wait a second between requests.

  2. Look at what came back. Print the HTML and search for your value. If it isn't there, the page builds it with JavaScript: look for a JSON blob or a data URL before you reach for Selenium.

  3. Pick the value. BeautifulSoup with a CSS selector (soup.select_one) to start with. lxml with XPath when you need speed.

  4. Write a row per page. One JSON line or CSV row per page, with its link, so a bad value can be traced back.

  5. Keep the pages. Save the HTML you fetched. You can fix a rule and run it again without asking the site twice.

Python web scraping tools, and when to use which

  1. requests + BeautifulSoup. The default. Easy to read, fine up to tens of thousands of pages.

  2. lxml or parsel. The same job, many times faster, with XPath.

  3. Scrapy. A full crawler: queues, retries, throttling. Worth it when fetching is the hard part.

  4. Selenium or Playwright. A real browser, for values that only exist after JavaScript ran. Seconds and hundreds of MB a page.

  5. Regular expressions. The fastest, with no page to parse first. Hard to write by hand so that they survive a redesign.

Why a Python scraper breaks after a month

  1. The selector was written on one page. It fits that page. The next one has a sale badge, no stock line or two prices, and the selector quietly reads the wrong thing.

  2. Class names change. Hashed classes like css-1x2y3z are new after every release of the site.

  3. Nothing tells you. The script keeps running and writes None, or worse, a value from another part of the page.

A free Python script that says when it is unsure

The script generator on this site writes one Python file with a rule per field and backups for each. Every value comes with how sure it is: sure, fallback, conflict or none. It needs Python 3.8 and the regex module, and reads a page, a file of links or a sitemap. The demo gives you a working script for its example shop, free. Rules for your own pages are learned from a few examples you send.

More on this

Tested on 207 real pages from 49 sites: 609 of 659 values exactly right, 0 wrong. The rest came back as not found, unsure or in another format.

Made by Simon. I have been scraping for more than five years and building software since 2015.

Try it on your own pages

Paste a few links and a couple of values. The look is free, and you hear the price before anything is due.

Try it on your pages

The first look is free, and you hear the price before anything is due. If I can't get your examples right, you don't pay.