Guides
Batch Extraction
Extract data from many pages using crawl then extract
6 min read
The client.crawl() orchestrator with extract_schema is the most efficient way to get structured data from an entire site — one call, no polling.
Overview
When you need data from many pages, you have three options:
- One-call approach (recommended with an SDK):
client.crawl()withextract_schema— handles discovery, fetching, and extraction automatically - Manual approach:
webgrph.crawl()thenkolektr.page()each page — for control over which pages you extract and how you handle errors - A crawl-extract job on the server (below): one request starts a crawl and extracts every page it fetched; you read the rows by cursor or download them as JSON, NDJSON or CSV. It keeps running when your program stops
One-call approach (recommended)
from kloakd import Kloakd
client = Kloakd(api_key="sk-live-...", organization_id="your-org-id")
result = client.crawl(
"https://example.com",
max_depth=2,
extract_schema={"title": "css:h1", "content": "css:article"},
)
print(f"Crawled {result.total_pages_discovered} pages")
for page in result.pages:
if page.structured_data:
print(f" {page.url}: {page.structured_data.get('title')}")
Streaming version
for event in client.crawl_stream(
"https://example.com",
max_depth=2,
extract_schema={"title": "css:h1", "content": "css:article"},
):
if event.type == "page_fetched":
print(f" fetched {event.url} ({event.page}/{event.total})")
elif event.type == "crawl_complete":
print("Done!")
Manual approach
from kloakd import Kloakd
client = Kloakd(api_key="sk-live-...", organization_id="your-org-id")
# Step 1 — Crawl the site (the SDK waits for the crawl and returns its pages)
crawl = client.webgrph.crawl("https://example.com", max_depth=2)
print(f"Found {crawl.total_pages} pages")
# Step 2 — Extract from each page
all_records = []
for page in crawl.pages:
data = client.kolektr.page(
page.url,
schema={"title": "css:h1", "content": "css:article"},
)
all_records.extend(data.records)
print(f"Total records: {len(all_records)}")
Each kolektr.page() call returns all the records it found on that page; there is no paging to do. Crawls larger than one response are read by cursor: see the Pagination guide.
Crawl-extract job
POST /api/v1/organizations/{org_id}/webgrph/crawl-extract crawls a site and extracts each crawled page from the HTML the crawl fetched, so no page is fetched twice. It needs Extract (Kolektr) as well as Crawl (Webgrph) in your plan, a live key (a test key answers 403 test_key_not_allowed), and one work slot. It is not in the API reference yet. The SDKs have methods for it from 0.7.0: webgrph.crawl_extract (start and wait), start_crawl_extract, get_crawl_extract_results, download_crawl_extract and cancel_crawl_extract in Python, and the same in camel case in TypeScript and Java (crawlExtract, startCrawlExtract, …) and Go (CrawlExtract, StartCrawlExtract, ListCrawlExtractResults, DownloadCrawlExtract, …); see each SDK page. The examples below call the API directly.
curl -X POST https://api.kloakd.dev/api/v1/organizations/$ORG_ID/webgrph/crawl-extract \
-H "Authorization: Bearer sk-live-..." -H "Content-Type: application/json" \
-d '{
"url": "https://books.toscrape.com",
"max_depth": 2,
"max_pages": 50,
"extract": {
"schema": {
"item_selector": "article.product_pod",
"fields": [
{"name": "title", "selector": "h3 a", "attribute": "title"},
{"name": "price", "selector": "p.price_color"}
]
}
}
}'
| Field | Type | Description |
|-------|------|-------------|
| url | string | Where the crawl starts (http:// or https://) |
| max_depth, max_pages | integer | As for a crawl: left out, your plan's maximum; given, capped at it |
| include_external_links, session_artifact_id, capture_mode, scope, budget | | As for a crawl |
| extract | object | What to extract from each page; all of it optional. schema: CSS selectors (item_selector, and fields of name, selector, attribute, type), tried first. entity_type (such as "products") and fields (a list of field names) describe what you want when you give no selectors. With nothing given, KLOAKD looks for each page's own repeated items |
It answers 202:
{
"success": true,
"job_id": "…",
"crawl_id": "…",
"url": "https://books.toscrape.com",
"status": "queued",
"limits": { "max_pages": 50, "max_depth": 2 },
"links": {
"status": "/api/v1/organizations/{org_id}/webgrph/crawl-extract/{job_id}",
"results": "…/crawl-extract/{job_id}/results",
"pages": "…/crawl-extract/{job_id}/pages",
"cancel": "…/crawl-extract/{job_id}/cancel",
"export": { "json": "…/export?format=json", "ndjson": "…/export?format=ndjson", "csv": "…/export?format=csv" },
"crawl_events": "/api/v1/organizations/{org_id}/webgrph/crawl/{crawl_id}/events",
"events": "/api/v1/organizations/{org_id}/pipeline/{job_id}/stream"
}
}
A start page that is not a public web address (a loopback, private or link-local address, a cloud metadata host, or a name that resolves to one) answers 400 URL_NOT_ALLOWED and no job is made (crawl errors).
Poll links.status (GET …/crawl-extract/{job_id}). status goes queued, crawling, extracting, then completed, failed or cancelled. progress counts pages_crawled, pages_total, pages_extracted, pages_failed and items. crawl says what the job's crawl really did, with the crawl's outcome fields: a completed job whose crawl has is_partial: true holds the rows of the pages that crawl fetched, not of the whole site. shapes lists each distinct set of fields among the rows, with its row count.
| Route | Returns |
|-------|---------|
| GET …/crawl-extract/{job_id}/results?limit=100&cursor=… | rows, total, count and next_cursor (null on the last page). limit is 1 to 1000. Each row has page_url, page_type, row_kind (item or page), item_index, data and shape. Filters: shape, page_type, kind |
| GET …/crawl-extract/{job_id}/pages?limit=100&offset=0&status=extracted | The crawled pages and how each was extracted; status is extracted or failed |
| GET …/crawl-extract/{job_id}/export?format=csv | The rows as a file: json (default), ndjson or csv, with the same filters |
| POST …/crawl-extract/{job_id}/cancel | Stops the job; the rows it has are kept. 409 when the job has already ended |
Rows appear as they are written, and a job that is still running, or that failed or was cancelled part way, exports the rows it has. The CSV has the columns page_url, page_type, row_kind, item_index, then the fields you asked for in the order asked, then every other field; lists and objects are written as JSON in their cell. The export's response headers say what the rows cover: X-Job-Status, X-Crawl-Status, X-Crawl-Partial, X-Crawl-Partial-Reason, X-Crawl-Pages-Discovered, X-Crawl-Pages-Fetched, X-Crawl-Pages-Unfetched, X-Crawl-Cancelled and X-Crawl-Root-Unfetched (a header is absent until the crawl has reported its value).
Export a batch job
A batch job (a list of URLs extracted as one job, as the app's batch extraction starts them) can be downloaded the same way:
curl -OJ "https://api.kloakd.dev/api/v1/organizations/$ORG_ID/batch-jobs/$BATCH_JOB_ID/export?format=csv" \
-H "Authorization: Bearer sk-live-..."
format is json (default), ndjson or csv; the file is named batch-{id}.{ext}. There is one row per extracted item, or one row for a URL that gave no items. Every row has url, job_id, status, row_kind and item_index; the extracted fields are in data (JSON, NDJSON) or in their own columns (CSV). A batch that is still running exports what it has; an unknown batch answers 404.
Handling errors in a batch
from kloakd.errors import KloakdError, QuotaExceededError
for page in crawl.pages:
try:
data = client.kolektr.page(page.url, schema={"title": "css:h1"})
except QuotaExceededError:
print("GB allocation used up; stopping")
break
except KloakdError as e:
print(f"{page.url}: {e}")
continue
The SDKs already retry rate limits (429) and 503s, honouring Retry-After; QuotaExceededError is not retried.
Tips
- Use
client.crawl()withextract_schema— one call replaces the entire crawl-then-extract loop - Reuse fetched pages — when you already fetched a page with
evadr.fetch(), pass itsartifact_idasfetch_artifact_id - Limit crawl depth —
max_depth=2is usually enough for most sites - Stop on
QuotaExceededError— further calls fail until a top-up or the next billing period
Next steps
- crawl() reference — full crawl orchestrator API
- Pagination guide — cursors and offsets
- extract() reference — full extract API
