Sub topic 14
Extracting custom data with XPath, CSS path and regex
The built-in tabs report what Screaming Frog already knows how to measure. Custom extraction lets you pull out anything else that sits in a page's HTML, such as Open Graph tags, schema markup, author names, prices, or tracking IDs. This chapter covers how to write extractors, which of the three query languages to choose, and how to check the results.
This chapter assumes you can read basic HTML and have run a crawl as described in the earlier chapters. You also need to know how the Configuration menu scopes a crawl. Familiarity with the idea of a CSS selector helps but is not required.
Custom extraction and custom search
Screaming Frog has two related features under Configuration > Custom.
- Custom Search checks whether a page contains, or does not contain, a given piece of text or a regex. It returns a count per page and is useful for questions like "which pages still mention the old brand name?"
- Custom Extraction returns the actual values it finds. It is the right choice when you want the data itself, not just a yes or no.
Use search to filter and extraction to collect. If you need both, set up both. They run in the same crawl.
How an extractor runs
Each extractor has four settings: a name, a query type, the query itself, and an extraction mode. For every crawled HTML page, the spider runs the query against the page source (or the rendered DOM, see below). It then writes the result into a column named after your extractor.
Choosing XPath, CSS path or regex
| Query type | Best for | Weak point |
|---|---|---|
| XPath | Attributes, conditions, counting elements, walking up and down the tree | Syntax is harder to read at first |
| CSS path | Quick selection of elements by class, ID or tag | Cannot count or test conditions |
| Regex | Text inside scripts, comments, or inline JSON that is not a clean element | Easy to over-match, and fragile when markup changes |
Start with the structured options. XPath and CSS path understand the document tree, so a minor change in whitespace or attribute order does not break them. Use regex when the data is not a proper element. Tracking IDs inside a script block are a typical case.
Writing an XPath extractor
XPath is a path language for HTML. // means "anywhere in the document", [...] is a filter, and @name selects an attribute.
To compare the Open Graph title with the page title, create an extractor named OG Title with the query below and the mode Extract Text.
//meta[@property='og:title']/@contentThe query finds every meta element whose property attribute equals og:title, then returns its content attribute. If a page has no such tag, the cell is empty. An empty cell is a finding in itself.
More examples you can adapt:
//link[@rel='alternate'][@hreflang]/@hrefreturns the hreflang target URLs.//h2with Extract Text returns the text of every H2.count(//h2)with Function Value returns the number of H2 elements as a single value. Function Value is meant for XPath functions such ascount().//script[@type='application/ld+json']with Extract Inner HTML returns the raw JSON-LD block, which you can validate elsewhere.
Extraction modes
The mode decides what is returned for each node the query selects.
| Mode | Returns | Use when |
|---|---|---|
| Extract HTML Element | The selected element including its own tags | You want to inspect the full markup |
| Extract Inner HTML | Everything inside the element, tags included | You want the contents of a container or script block |
| Extract Text | Text only, tags stripped | You want a readable value such as a heading or a price |
| Function Value | The result of an XPath function | You are using `count()`, `string-length()` and similar |
A query that ends in /@attribute already returns a plain value, so Extract Text is the usual pairing.
CSS path extractors
A CSS path uses the same selectors you would write in a stylesheet. To collect the author name from <span class="author-name">, set the type to CSSPath and enter:
span.author-nameWith CSS path you can add an attribute in a separate field. For example, the selector link[rel='canonical'] with the attribute href returns the canonical URL. Without an attribute, the extractor returns text or HTML according to the mode you chose.
Regex extractors
A regex extractor returns what is matched by the capture group, the part of the pattern in parentheses. Include exactly one group for the value you want. To find a Google Analytics 4 measurement ID in the source, use:
(G-[A-Z0-9]{6,12})Without a capture group you can get empty or unexpected results, so test every pattern. A pattern like price: (\d+\.\d{2}) extracts only the number and ignores the label around it. Keep patterns as narrow as you can. A loose .* between two anchors tends to swallow large parts of the page.
Rendering matters
By default the spider reads the raw HTML the server sends. If your target value is inserted by JavaScript after the page loads, the extractor sees nothing. Switch to JavaScript rendering under Configuration > Spider > Rendering, then recrawl. This is slower, so only enable it when raw HTML does not contain the value. A quick check is to open View Source in a browser and search for the value. If it is missing there but visible on the page, you need rendering.
Testing before a full crawl
Do not run a full crawl to debug a query. Instead:
- Crawl a single URL in Spider mode, or use a short list.
- Open the Custom Extraction tab and read the column.
- Fix the query, clear the crawl and run again.
The extractor editor also shows a green tick for a valid query and a red cross for a syntax error. A valid query can still return nothing, so always check the data.
Common errors
- Empty column for every page. The value is probably added by JavaScript, or the query uses the wrong quotes or attribute name.
- Values only on some pages. The markup differs between templates. Check a failing page's source and widen the query, for example with an
orcondition in XPath. - Several values joined together. The query matches multiple nodes. Narrow it with a filter or an index such as
(//h2)[1]. - Regex returns the whole match, or nothing. The pattern has no capture group, or has more than one.
- Extraction on non-HTML URLs. Images, PDFs and CSS files have no DOM, so those rows will be blank. This is expected.
Using the results
Extractor columns appear in the Custom Extraction tab, and you can export them. Because they are normal CSV columns, you can process them with a script. This example flags pages whose Open Graph title differs from the page title. It assumes you exported the Internal tab and that the extractor is named OG Title. Check your file's header row, because exact column names depend on your extractor name and how many matches a page has.
import csv
with open("/tmp/sf-crawl/internal_all.csv", newline="", encoding="utf-8-sig") as f:
reader = csv.DictReader(f)
og_col = next(c for c in reader.fieldnames if c.startswith("OG Title"))
for row in reader:
title = row["Title 1"].strip()
og = row[og_col].strip()
if og and title and og != title:
print(row["Address"])
print(" title:", title)
print(" og: ", og)Extractors are saved with a configuration file, so you can reuse them across crawls and in command-line runs.
Exercise
A page shows its price in the browser, but your XPath extractor returns an empty cell for every product URL. View Source does not contain the price. What is the likely cause, and what do you change?
Show answer
The price is inserted by JavaScript after the page loads, so the raw HTML the spider reads does not contain it. Enable JavaScript rendering under Configuration > Spider > Rendering and recrawl, then check the Custom Extraction tab again.
Exercise
You want one number per page showing how many H2 headings it has. Which query type, query and extraction mode do you use?
Show answer
Use XPath with the query count(//h2) and the extraction mode Function Value.
Exercise
Your regex (G-[A-Z0-9]{6,12}) is meant to return the GA4 ID, but you want to be sure only the ID is returned, not surrounding text. Which part of the pattern controls that?
Show answer
The capture group, meaning the parentheses. The extractor returns only the text matched inside the group, so any text matched outside the parentheses is left out.