Skip to main content
scrapai is a database-backed scraping orchestration layer built on Scrapy. AI agents generate JSON spider configs instead of writing Python files.

Analysis and Review

During site analysis, agents write detailed notes in sections.md documenting URL patterns, site structure, and extraction strategy. Review the analysis, correct assumptions, and refine the approach before finalizing configs.

Full Control

Write configs by hand, edit generated ones, override settings per spider, or write custom callbacks with your own CSS/XPath selectors:

Team Benefits

All configs follow the same schema. Uniform structure across the fleet means easier code review, debugging, and onboarding. One developer can pick up another’s spider without decoding personal style choices.

Architecture

scrapai is an orchestration layer on top of Scrapy. Instead of writing a Python spider file per website, an AI agent generates a JSON config and stores it in a database. A single generic spider (DatabaseSpider) loads any config at runtime.

Component Overview

Codebase

Small and readable: ~4,000 lines of code. Built on Scrapy, SQLAlchemy, Alembic — tools you already know. Read the whole thing in an afternoon. Measured with pygount, counting actual code lines only (no blanks, no comments, no docstrings). Tests, examples, and docs excluded. Compare this to other scraping frameworks:
  • Scrapling: 5,875 lines (21% comments)
  • crawl4ai: 26,850 lines (21% comments)
scrapai is intentionally small. The complexity lives in Scrapy, SQLAlchemy, and the extraction libraries. Our contribution is the orchestration.

Writing Spider Configs

Here’s what an AI-generated spider config looks like:
You can write this by hand, no AI needed. See Spider Schema for the complete specification.

Custom Extractors

For non-article content (products, jobs, listings), write custom callbacks with field-level selectors:
See Custom Callbacks for complete examples.

Database Schema

All configuration lives in PostgreSQL (or SQLite for development):

Spider Table

SpiderRule Table

SpiderSetting Table

ScrapedItem Table

Extending scrapai

Adding a New Extractor

Create a new extractor class in core/extractors.py:
Register it in the extraction chain:

Adding Custom Middleware

Add middleware to middlewares.py:
Enable it in scrapy_settings.py:

Adding CLI Commands

Add commands to cli/:
Register in cli/__init__.py:

Storage Modes

Test mode (--limit N): saves to database, inspect via show command
Production mode (no limit): exports to timestamped JSONL files, enables checkpoint

Migrating Existing Scrapers

Point the agent at your existing Python scripts (Scrapy spiders, BeautifulSoup, Scrapling, whatever) and it’ll read them, understand the extraction logic, and write the equivalent scrapai JSON config.
Your existing scrapers keep running while you verify. No big bang migration required.

Security

All input is validated through Pydantic schemas. Spider configs, URLs, and settings are validated before touching the database or crawler. SQL queries use parameterized bindings. scrapai uses a config-only architecture where agents write JSON, not code. See Security-First Design for the full security model.

Contributing

Contributions welcome. Areas where help would be particularly valuable:

Structural Change Detection

Automatic detection of website structural changes

Extraction Modules

Additional extraction modules (images, tables, PDFs)

Anti-Bot Support

Anti-bot support beyond Cloudflare

Authentication

Authentication and session management

Development Setup

Running Tests

Code Style

We follow PEP 8 with these exceptions:
  • Line length: 120 characters
  • Docstrings: Google style

Limitations

Current limitations (pull requests welcome):
  • Authentication: No login support, no paywall bypass, no persistent sessions
  • Advanced anti-bot: We handle Cloudflare. Not DataDome, PerimeterX, Akamai, or CAPTCHA-solving services
  • Interactive content: No form submission, no click-based pagination
The codebase is designed to be extended. The crawling infrastructure is done; what’s missing is mostly parsing logic for additional content types.

Architecture

Technical architecture and design decisions

Spider Schema

Complete JSON schema reference

Custom Callbacks

Write custom field extractors

Security

Security model and validation