Web scraping in Python, step by step
Fetch the page. requests.get(url).text is enough for most pages. Send a clear User-Agent and wait a second between requests.
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.
Pick the value. BeautifulSoup with a CSS selector (soup.select_one) to start with. lxml with XPath when you need speed.
Write a row per page. One JSON line or CSV row per page, with its link, so a bad value can be traced back.
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
requests + BeautifulSoup. The default. Easy to read, fine up to tens of thousands of pages.
lxml or parsel. The same job, many times faster, with XPath.
Scrapy. A full crawler: queues, retries, throttling. Worth it when fetching is the hard part.
Selenium or Playwright. A real browser, for values that only exist after JavaScript ran. Seconds and hundreds of MB a page.
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
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.
Class names change. Hashed classes like css-1x2y3z are new after every release of the site.
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.
- Cut web scraping costs: stop paying an AI per page
- Free web scraping script generator
- Web scraping with JavaScript and Node.js
- XPath vs CSS selector vs regex
- Try it on your own pages
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.