Skill Spotlight: Crawlee Python for Reusing Learned Extraction Selectors
In Short
Crawlee Python can learn extraction selectors and reuse them across matching pages and later runs. The useful operator question is whether a retained selector cache produces source-correct records with less observed model usage. Start with a small authorized sample, keep the storage, and compare a second run before expanding the crawl.
This spotlight follows ASEβs November 12 addition, Learn and reuse route-specific extraction selectors with Crawlee Python. It focuses on selector reuse and factual extraction checks. Provider selection and recurring CSV drift are separate decisions.
Who this is for
This is for Python operators collecting repeated fields from server-rendered article or catalog pages. Choose a site you are authorized to access and a record you can verify manually. A useful pilot has several pages sharing a layout and at least one known layout variant.
Define acceptance before running: which fields matter, how missing values should appear, which pages are allowed, and who reviews discrepancies. For an article record, ask whether the extracted body includes the whole article or accidentally captures navigation, related stories, or a subscription notice.
Starter workflow
- Pin the reference. Use an isolated environment and the release-specific installation
python -m pip install 'crawlee[pydantic-ai]==1.11.2'. Keep the lockfile and Python version with the run notes. Follow the pinned selector example; replace its literal credential placeholder with environment-backed provider configuration. Never put credentials in a shared script. - Specify the record and route. Begin with a Pydantic schema containing title and article text. In the example, the default handler enqueues matching article URLs under an article label; the labeled handler calls
context.extract(Article)and savesarticle.model_dump()usingcontext.push_data(). Replace the exampleβs seeds and URL pattern with your authorized sample. Set a small request ceiling, such as 10 requests. - Configure both paths. The example supplies a
PydanticAiSelectorExtractorwith an explicitPydanticAiDirectExtractorfallback. Apply per-model-runUsageLimitsto both extractors, adapting the official usage-limit example. Retain cumulative usage and log budget exceptions, skipped URLs, and other errors. Use conservative concurrency while learning the workload. - Inspect the first output. Check a proposed sample of five records against their original pages, including the variant. Compare actual text boundaries and identifiers, not just whether fields are populated. Keep discrepancies as review evidence; do not quietly replace bad records and count the run as clean.
- Retain storage and rerun. Keep the same Crawlee storage, including its selector KeyValueStore. Make sure the intended URLs are actually processed again; a retained request queue should not turn the comparison into an empty run. Preserve the selector cache while deliberately managing request state. Repeat the same source checks and record what changed.
| Stage | Review evidence |
|---|---|
| Schema and authorized pages | Field definitions and bounded URL list |
| Learn or reuse selectors | Route bucket, retained cache, observed model usage |
| Direct fallback when needed | Fallback outcomes, skipped URLs, errors |
| Verify source values | Reviewed records and rerun comparison |
Use a comparison sheet with one row per run: processed URLs, accepted records, discrepancies, failed or skipped URLs, model requests, input tokens, output tokens, and total tokens. Leave usage cells empty until measured. Compare equivalent completed work; a smaller token total accompanied by missing articles is a failed extraction comparison.
Recommended ASE skills
Keep the Python selector workflow above central. Choose these separate routes only when the input or downstream task calls for them:
- Crawlee web crawling and scraping SDK: a separate Node.js option for teams working in that runtime.
- Playwright Page Data Extractor: a browser alternative when required content depends on JavaScript rendering.
- qsv CSV profiling: an optional later check after you manually prepare a local CSV export.
- DuckDB SQL Analytics Agent: an optional local analysis route for manually prepared export files and comparison queries.
What to watch
The v1.11.2 concept guide marks this API experimental. Request labels supply default cache buckets; cache_tag can separate layouts explicitly. Matching selectors avoid another model call; unmatched layouts may learn another variant. Unsupported schema shapes, including dictionary fields, need fallback or a different extraction approach.
Schema validation checks structure, not factual correctness. Layout drift can leave a selector matching the wrong text. Review representative values after site changes. PydanticAiCrawler uses HTTP without JavaScript rendering, so confirm the needed content exists in fetched HTML before debugging selectors.
Retries can add model costs. Separate permanent configuration failures from recoverable page failures. Cumulative token checks and per-run limits do not guarantee a hard spending cap: concurrent work can already be in flight. Report observed usage and incomplete work together, with no invented savings percentage.
FAQ
Does a warm cache prove extraction is correct?
No. It proves nothing about whether a matching element contains the intended value. Compare sampled output with source pages on both runs.
Should every page share one cache bucket?
Group pages intentionally by route and expected layout. Investigate variants rather than treating a shared label as proof of identical structure.
What should the pilot deliver?
Return the schema, authorized URL list, retained cache location, reviewed records, errors, and measured rerun comparison. Expand only after the source checks pass.
