Extruct is a Python library for extracting embedded metadata from HTML markup. It currently supports W3C's HTML Microdata, embedded JSON-LD, Microformat (via mf2py), Facebook's Open Graph, experimental RDFa (via rdflib), and Dublin Core Metadata (DC-HTML-2003). The library is actively maintained with its current stable version being 0.18.0.
pip install extructVerified import paths — ran on the pinned version, not inferred.
This quickstart fetches HTML content from a URL, determines the base URL for resolving relative paths, and then uses `extruct.extract` to retrieve structured metadata in common formats (JSON-LD, Microdata, Open Graph). The `uniform=True` parameter ensures a consistent output structure for easier processing.
Upgrade `extruct` to version 0.18.0 or newer. If an upgrade is not feasible, temporarily pin `lxml` to a version less than 5.1.0 (e.g., `pip install lxml<5.1.0`).
Use the `uniform=True` parameter in `extruct.extract()` to ensure a more consistent output structure (e.g., always a list). Always check the type of the returned data before attempting to access elements by index or key.
To optimize performance and resource usage, specify only the required syntaxes using the `syntaxes` parameter (e.g., `syntaxes=['json-ld', 'opengraph']`).
Install `extruct` with the `cli` extra to include `requests`: `pip install 'extruct[cli]'`.
Update `extruct` to version 0.18.0 or later. If updating `extruct` is not possible, downgrade `lxml` to a version prior to 5.1.0 (e.g., `pip install lxml==5.0.1`).
First, inspect the source HTML for the presence of Microdata, JSON-LD, Open Graph, etc. Second, always provide the `base_url` parameter to `extruct.extract(html_string, base_url=actual_url)` to ensure proper resolution of relative URLs and images.
Install `extruct` with its command-line interface dependencies using `pip install 'extruct[cli]'`.