Skip to main content
Migrate existing scrapers from Scrapy, BeautifulSoup, Scrapling, or any Python scraping framework to scrapai’s database-driven architecture.

Overview

Migrating to scrapai means converting your Python scraping code into JSON configs. The process:
  1. Analyze existing code to understand extraction logic
  2. Map to scrapai concepts (rules, extractors, callbacks)
  3. Generate JSON config with equivalent behavior
  4. Test and verify extraction quality
  5. Deploy to database and retire old code

Why Migrate?

From README.md:170-179:
Your existing scrapers keep running while you verify. No big bang migration required.
Benefits:
  • Database-first management: Change settings across 100 spiders with one SQL query
  • Uniform structure: Consistent schema, validation, naming conventions
  • Built-in features: Cloudflare bypass, checkpoint, proxy escalation, incremental crawling
  • Easy to review: JSON configs are easier to audit than Python code
  • AI-assisted updates: Point an agent at a broken spider to auto-fix extraction rules

Migration Workflow

Using an AI agent (Claude Code, Cursor, etc.):

Manual Migration

For direct control:
  1. Read your existing spider code
  2. Extract URL patterns, selectors, and extraction logic
  3. Write equivalent JSON config (see examples below)
  4. Import: ./scrapai spiders import config.json --project myproject
  5. Test: ./scrapai crawl spider_name --project myproject --limit 5
  6. Compare output with original spider
  7. Iterate until quality matches

Scrapy Spider Migration

Original Scrapy Spider

scrapy_spider.py

Equivalent scrapai Config

bbc_config.json

Key Mappings

BeautifulSoup Migration

Original BeautifulSoup Script

bs4_scraper.py

Equivalent scrapai Config

products_config.json

Key Differences

scrapai provides automatic request handling, link extraction, retries, rate limiting, and JSONL export - eliminating manual boilerplate code.

Scrapling Migration

Migrate from Scrapling when managing 10+ sites with similar structure. Keep Scrapling for single sites with complex interactions, heavy JavaScript, or login flows.

Example Migration

scrapling_script.py
hn_config.json

Processors for Data Cleaning

From core/schemas.py:131-156:

Common Processor Patterns

Strip whitespace:
Remove characters:
Extract with regex:
Convert type:
Join list:
Default value:
Lowercase:
Parse datetime:

Validation During Migration

All configs are validated before import:
  • Spider names: Alphanumeric characters, underscores, and hyphens only
  • URLs: HTTP/HTTPS only, with SSRF protection (blocks localhost and private IPs)
  • Callbacks: Must be valid Python identifiers, cannot use reserved names (parse, parse_article, etc.)

Testing After Migration

Compare Output Quality

Verify Extraction Rules

Performance Comparison

Incremental Migration Strategy

Phase 1: Pilot (1-2 weeks)

  1. Pick 3-5 representative spiders
  2. Migrate to scrapai
  3. Run both old and new in parallel
  4. Compare output quality
  5. Tune extraction rules until quality matches

Phase 2: Batch Migration (2-4 weeks)

  1. Group remaining spiders by similarity
  2. Migrate one group at a time
  3. Reuse patterns from pilot spiders
  4. Test each batch before moving to next

Phase 3: Cutover (1 week)

  1. Switch production traffic to scrapai
  2. Keep old spiders as backup for 1 month
  3. Monitor error rates and data quality
  4. Retire old code once confident

Phase 4: Optimization (ongoing)

  1. Tune DOWNLOAD_DELAY and CONCURRENT_REQUESTS
  2. Enable DeltaFetch for incremental crawling
  3. Schedule recurring crawls with cron + Pueue (detached runs)
  4. Add custom callbacks for edge cases

Common Pitfalls

Regex Patterns: Copy patterns directly from Scrapy, then test with ./scrapai crawl --limit 5. Relative URLs: Scrapy handles urljoin() automatically. Use css: "a::attr(href)". Custom Middleware:
  • Proxy rotation: Use built-in proxy escalation
  • Cloudflare: Enable CLOUDFLARE_ENABLED: true
  • Custom headers: Add to spider settings
JavaScript/Dynamic Content: Use scrapai’s Playwright extractor:

See Also

Custom Callbacks

Write custom extraction logic for complex sites

Security

Understanding config validation and SSRF protection