Skip to main content

Guides

Pagination

Next pages of an extraction, crawl pages by cursor, API data by offset

4 min read


Overview

Two results come in pages: the pages of a crawl (cursor-based) and the records of a detected API (offset-based). The rows of a crawl-extract job are read by cursor too (…/results?cursor=…, next_cursor until it is null). An extraction (kolektr.page()) returns all of its records in one response; with pagination it follows a site's "next page" links first (Extractions).

Crawl pages: cursor

GET /api/v1/organizations/{org_id}/webgrph/crawl/{crawl_id}/pages?limit=500&cursor=… returns nodes, total and next_cursor; pass next_cursor back until it is empty. limit is 1 to 1000 (default 100); a cursor that is not one the API returned (not a non-negative integer) answers 422. By default the list leaves out the folders the site tree groups pages under, which were never fetched; add include_grouping=true to get them too (Crawl pages). A crawl's pages are kept for 2 hours; after that, or for an unknown crawl_id, the call answers 404 ("not found or expired").

from kloakd import Kloakd

client = Kloakd(api_key="sk-live-...", organization_id="your-org-id")

crawl_id = client.webgrph.start_crawl("https://example.com", max_depth=3)
# ... wait for it (get_crawl_status) ...

cursor = None
while True:
    page = client.webgrph.get_crawl_pages(crawl_id, cursor=cursor, limit=500)   # a dict
    for node in page["nodes"]:
        print(node["url"], node["depth"])
    if not page.get("next_cursor"):
        break
    cursor = page["next_cursor"]

Or let the SDK do it:

for node in client.webgrph.crawl_all_pages(crawl_id):
    print(node.url, node.depth)

for node in client.webgrph.crawl_all("https://example.com", max_depth=3):   # crawl and read
    print(node.url)
const crawl = await client.webgrph.crawl('https://example.com', { maxDepth: 3 });
const pages = await client.webgrph.crawlAllPages(crawl.crawlId);

API data: offset and limit

The records a detected API returned are read with offset and limit (default 1000, at most 10000). Each response has records and pagination (offset, limit, returned, total_available, has_more):

offset = 0
while True:
    data = client.kolektr.get_api_data_paginated(
        "https://example.com/api/v1/products", offset=offset, limit=1000,
    )
    for record in data["records"]:
        print(record)
    if not data["pagination"]["has_more"]:
        break
    offset += data["pagination"]["returned"]

The endpoint must have been found by a crawl first (detected APIs); otherwise the call answers 404 ("API endpoint not found in cache. Please run discovery first.").

Extractions

result = client.kolektr.page(
    "https://example.com/products",
    schema={
        "item_selector": ".product",
        "fields": [
            {"name": "name", "selector": ".product-name"},
            {"name": "price", "selector": ".product-price"},
        ],
    },
)
print(result.total_records, len(result.records))   # every record, one response

To read a listing that spans several pages, add pagination and max_pages to the schema. The extraction follows each page's next-page link (rel=next, a "Next" link, or the next number in the pagination block; same host only) and extracts up to max_pages pages, the first included, with the same schema:

result = client.kolektr.page(
    "https://books.toscrape.com/catalogue/page-1.html",
    schema={
        "item_selector": "article.product_pod",
        "fields": [{"name": "title", "selector": "h3 a", "attribute": "title"}],
        "pagination": True,
        "max_pages": 3,
    },
)
print(result.pages_scraped, result.total_records)   # 3 pages, 60 records

Each next page is checked against robots.txt first. Paging stops early at a page with no next page, one already read, one robots.txt does not allow, one that fails, or one with no records; the records already read are kept. Load-more buttons and infinite scroll are not followed. Paging needs a page KLOAKD fetches: with a fetch_artifact_id it answers 422 pagination_needs_fetch. The details are on extract(). For a whole site, crawl it with an extract_schema.

page_all() is the same call, kept for code written against 0.2.

List calls

The SDKs' list calls take the API's paging parameters (from 0.5.0):

| Call | Parameters | |------|------------| | kolektr.list_jobs / listJobs | page, limit, status_filter | | kolektr.list_scrapers / listScrapers | page, page_size, is_template | | kolektr.list_content / listContent | page, page_size, search |

page starts at 1 and limit or page_size is 1 to 100 (default 20); a value outside those answers 422. The api-data calls also take endpoint_hash, and extract_all_api_data takes max_records.

Rate limit awareness

Where a request-rate limit applies, each request counts against it; larger limit values mean fewer requests.

Next steps

Was this page helpful?