AI and automation
Can an LLM replace selectors for data extraction?
For some jobs, yes: a language model can pull fields out of pages without hand-written selectors and copes with layout changes, but it costs more per page and needs validation.
Where LLMs shine
Selectors such as CSS and XPath are precise and cheap, but they break when the layout changes, and writing them for hundreds of different sites is slow. A language model reads the page much as a person does: "find the price, the currency, the stock status". It handles sites you have never seen, tolerates redesigns, and can normalise messy text into a schema. That makes it attractive for long-tail sources, one-off research, and pages whose structure varies from item to item.
Where selectors still win
- Cost and speed. A selector runs in microseconds for free. A model call costs tokens and takes seconds, multiplied by every page.
- Exactness. Selectors return what is on the page or nothing. Models can produce a plausible value that is not there, especially when the field is missing.
- Volume. For a million pages from one site with a stable layout, selectors are the obvious choice.
- Auditability. When a selector is wrong, it is wrong the same way every time, which is easy to debug.
A practical hybrid
Many teams combine both. Use selectors or structured data (JSON-LD, embedded JSON) wherever they exist, and fall back to a model only for fields the selectors miss or for unfamiliar sites. Another pattern uses the model once per site to propose selectors, which a person reviews and then runs cheaply at scale.
import json
SCHEMA_HINT = '{"title": str, "price": "number or null", "currency": "ISO code or null", "in_stock": "bool or null"}'
def extract(page_text, call_model):
prompt = (
"Extract these fields from the product page text. Use null when a field is not present; "
f"never guess. Reply with JSON only, shaped like {SCHEMA_HINT}.\n\n" + page_text[:12000]
)
record = json.loads(call_model(prompt))
if record.get("price") is not None and str(record["price"]) not in page_text.replace(",", ""):
record["price_unverified"] = True # value not found verbatim on the page
return record
Keep the model honest
Ask for null when a value is missing, ask for JSON matching a fixed schema, and verify. A cheap check is to confirm that extracted strings and numbers appear in the source text. Validate types and ranges (a negative price is wrong), and sample results for manual review. Feed the model cleaned text or the relevant part of the page, not the whole HTML with scripts and styles, which wastes tokens and distracts it.
Getting the page in the first place
Neither selectors nor models help if the page you fetched is a block page. Before extraction, check that you received real content: a CAPTCHA, a Cloudflare interstitial or a consent wall will be "extracted" into nonsense by a model that tries to be helpful. Detect challenges in code, solve them where appropriate, for example with a Cloudflare Challenge task for a full Cloudflare screen, and only then pass the page on. The article on AI agents in web scraping discusses this pipeline.
Cost arithmetic
Estimate tokens per page after cleaning, multiply by your page count and the model's price, and compare with the engineering time selectors would take. For a few hundred pages across many sites, the model often wins. For steady, large volumes on few sites, selectors almost always do. Revisit the choice as volumes change.
Data and privacy
Sending pages to a hosted model means sending their content to a third party. Avoid pages with personal data unless you have a lawful basis and an agreement with the model provider, and stick to the same public data rules that apply to any data extraction. The Crawl4AI guide shows one open source tool that combines crawling with model based extraction.
Common questions
Will an LLM make up values?
It can, especially for missing fields. Ask for null when absent and check values against the page text.
Is model extraction cheaper than writing selectors?
For many small sources, often yes; for large volumes from a few sites, selectors are far cheaper.
Should I send raw HTML to the model?
Send cleaned text or the relevant section; scripts and styles waste tokens and add noise.
Can a model write selectors for me?
Yes. Generating selectors once per site and reviewing them combines the model's flexibility with selector speed.
What if the page is a CAPTCHA screen?
Detect it before extraction and solve it first, or the model will extract nonsense from the challenge page.
Keep reading
- Data extractionData extraction turns raw HTML or JSON into clean, typed records with a fixed schema that downstream tools can trust.
- How AI agents are used in web scrapingWhere language model agents genuinely help a scraper, from planning crawls to messy extraction and selector repair, and where they fall short.
- Crawl4AIUse Crawl4AI hooks to solve a widget after navigation or preload a WAF cookie before it, so crawls return real content.
- Structured dataJSON-LD, microdata and RDFa blocks that describe a page in machine readable form, and why they are the first thing to parse.