Extract data from static pages
This guide shows how to load an HTML page, find elements with CSS selectors, extract text and attributes, and return structured data.
Static extraction does not need a browser. Ferret fetches the HTML over HTTP and parses it in memory, which is fast and lightweight. Use this approach whenever the page content is present in the initial HTML response. For pages that load content through JavaScript, see Browser-driven pages.
Open a page
Use WEB::HTML::OPEN to fetch and parse an HTML page:
The function returns an HTML page value. You can read properties like title directly on it.
Query elements
Use the query expression to find elements:
This returns all matching elements as an array.
To get a single element, use QUERY ONE:
More about query expressions see the documentation.
Extract text and attributes
Once you have an element, read its properties:
Common element properties:
| Property | Description |
|---|---|
textContent |
The text content of the element |
innerHTML |
The inner HTML of the element |
attributes |
An object of attribute key-value pairs |
attributes.href |
A specific attribute value |
Use array operators for compact extraction
The [*] array operator lets you project fields from a list of elements without writing a FOR loop:
You can also filter inline:
Query nested elements
When a page has repeating structures — product cards, table rows, list items — query the container first, then query inside each one:
NOTE: For simple queries, you can use the shortcut query syntax. For details and limitations, see Shortcut syntax.
The ?. optional chaining operator returns NONE instead of failing when an element is not found. This keeps the script running even when some cards are missing a field.
Handle missing elements
Not every page has the elements you expect. Use QUERY EXISTS to check before extracting, or ON ERROR RETURN to provide a fallback:
For more error handling patterns, see Error handling and resilience.
Filter and sort results
Use FILTER, SORT, and LIMIT inside a FOR loop to shape the output:
Use parameters for reusable scripts
Save a script to a file and pass the URL as a parameter:
echo 'LET page = WEB::HTML::OPEN(@url)LET headers = QUERY 'h1, h2, h3' IN page USING cssRETURN headers[*].textContent' > headings.fql
Run it with any URL:
ferret run headings.fql --param url=https://mockery.ferretlang.org