What each one can do
| Can it… | CSS selector | XPath | Regex |
|---|---|---|---|
| Pick by tag, class or id | yes | yes | yes, as text |
| Pick by the text inside | no | yes | yes |
| Walk up to a parent | only with :has() | yes | no |
| Read part of a value, like 8,99 from "€ 8,99 incl." | no | barely | yes |
| Read JSON inside a script tag | no | no | yes |
| Work without parsing the page first | no | no | yes |
Which is faster?
A regular expression reads the raw text and stops at the first match, with no tree to build. CSS and XPath need the whole page parsed first, and that is where most of the time goes. On a few hundred pages you won't notice. On millions, parsing is the bill.
Why hand-written selectors and regexes break
Written on one page. It fits the example. Only more pages show which part of it was luck.
Tied to class names. div.css-1x2y3z is gone after the next release. The words near the value ("Price", "In stock") usually stay.
Tied to a position. The third span, the second table row: one extra badge and you read the neighbour.
Silent. A selector that matches nothing returns nothing. One that matches the wrong thing returns a value, and nobody notices.
Which should you use?
CSS selector. The default when you write by hand: short and easy to read.
XPath. When you need the text inside an element, or its parent.
Regex. For a piece of a value, JSON in a script tag, or speed. Hard to write well by hand, easy for a machine to grow from examples and test.
Regex rules, grown from examples
That last point is what this site does. From a few example pages it grows a regular expression per field until it fits every one, adds backups, and tells you per value whether the main rule matched. You get the speed of regex without writing one.
- Cut web scraping costs: stop paying an AI per page
- Web scraping with Python
- Free web scraping script generator
- You probably don't need a headless browser
- 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.