Skip to content
DuckDB community extension

html_query

Query HTML using CSS selectors, extract JSON from LD+JSON, JavaScript variables, and Next.js RSC

01 / SETUP

Install and load

Run these commands in a Haybarn SQL session. The extension is available from the community repository.

Haybarn 2.0.0 Β· SQL
INSTALL html_query FROM community;
LOAD html_query FROM community;

Using upstream DuckDB? Follow the maintainer’s installation instructions.

02 / QUERY

Usage example

Extract text from first matching element

SQL session and output
-- Extract text from first matching element
SELECT html_query('<html><title>Hello World</title></html>', 'title', '@text');
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ html_query  β”‚
β”‚   varchar   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Hello World β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

-- Extract all matching elements as VARCHAR[]
SELECT html_query_all('<div><p>First</p><p>Second</p></div>', 'p', '@text');
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ html_query_all β”‚
β”‚   varchar[]    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [First,Second] β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

-- Extract JSON from LD+JSON scripts (returns array)
SELECT html_extract_json(
  '<script type="application/ld+json">{"name":"Product"}</script>',
  'script[type="application/ld+json"]'
);
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    html_extract_json     β”‚
β”‚         varchar          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [{"name":"Product"}]     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Provided by midwork-finds-jobs. Not yet verified on Haybarn 2.0.0.

Sample data & setup

About this extension

html_query is a DuckDB extension for querying HTML using CSS selectors.

The extract parameter specifies what to extract from matched elements:

Project overview from the community descriptor.

Documentation and resources

Project and build details
Repository
midwork-finds-jobs/duckdb_html_query
License
MIT
Maintainers
onnimonni
Written in
Rust
Build source
87af2dc57ae7
Build definition
Community descriptor