Skip to main content

Modules

Kolektr

Structured data extraction — the engine behind extract()

4 min read


Kolektr is the extraction module that powers kolektr.page(). It extracts structured data from a web page with CSS selectors, or from the page's own structured data for fields named without one.

The high-level client.crawl() orchestrator can extract structured data from every page on a site in a single call — pass an extract_schema and Kolektr runs automatically on each fetched page.

For the full API reference, see kolektr.page().

Pass fetch_artifact_id from a previous evadr.fetch() call to avoid re-fetching pages behind anti-bot protection.

page

result = client.kolektr.page(
    "https://example.com/products",
    schema={
        "item_selector": ".product",          # one record per product
        "fields": [
            {"name": "name", "selector": ".product-name"},
            {"name": "price", "selector": ".product-price"},
            {"name": "rating", "selector": ".product-rating"},
        ],
    },
    fetch_artifact_id=page.artifact_id,
)
if not result.success:
    print("Extraction failed:", result.error)   # still HTTP 200: check success
print(result.total_records)
for record in result.records:
    print(record)

All records come back in one response. result.method says how they were extracted: fetched_content_artifact (from the fetch_artifact_id), api_map_artifact, level1, level2, in_process_html, or preflight_auth_detect when the page showed a login form instead. The API response also says which level a fresh scrape ran at and why (level, level_reason): see extract().

extract_html

Extract from HTML you already have, without fetching:

result = client.kolektr.extract_html(
    html="<html>...</html>",
    url="https://example.com/article",
    schema={"title": "h1", "body": "article"},
)
print(result.records)

Schema

The shorthand maps each field name to a CSS selector, {"price": "css:.price"} (the css: prefix is optional); every SDK converts it from 0.5.0. For an attribute instead of the text, or a field type, pass the full form:

schema={
    "fields": [
        {"name": "title", "selector": "h1"},
        {"name": "image", "selector": "img.hero", "attribute": "src"},
    ]
}

List pages: item_selector

A schema without item_selector reads the whole page as one record: on a list page each field holds its first match, or every match as a list. Add item_selector, the CSS selector of the repeated element, for one record per item, with each field's selector read inside it (no match: null; no selector: the item element itself). See One record per item for the rules and the books.toscrape.com example (20 records).

Fields without a selector

A field given by name only, {"name": "price", "type": "number"}, is read from the page's JSON-LD, microdata or RDFa, then its meta tags, then (for title, heading, description, url) its <title>, <h1>, meta description and canonical link; null when the page gives none. See Fields without a selector: in automatic mode a request whose fields all lack a selector runs at Level 2, which does not read them this way, so name level="level1".

Next pages: pagination

With "pagination": True and "max_pages": 3 in a full schema (a dict with a fields list), a page Kolektr fetches itself is followed to its next pages (a rel=next or "Next" link, or the next number in the pagination block, same host only), each checked against robots.txt first, and every page's records come back together; pages_scraped counts the pages. With a fetch_artifact_id or in extract_html it answers 422 pagination_needs_fetch. Load-more buttons and infinite scroll are not followed. See Next pages.

attribute and type are applied with or without item_selector, whether Kolektr fetches the page itself, reads a fetch_artifact_id or runs extract_html; without item_selector a field with an attribute gives that attribute of every match, as a list. A Level 2 extraction (level: "level2") that renders the page in a browser applies them to the page it rendered; when item_selector matches nothing there, Level 2's own records are kept, typed. When the run is made by the strategy engine (method: "level2_se"), the same applies to the page the crawler sends back; if no page comes back, only type is applied.

Long extractions

An extraction still running after about 85 seconds answers 202 with a job to poll; see extract().

Next steps

Was this page helpful?