Skip to main content
Callbacks extract custom fields from structured data like products, jobs, listings, or forums. They define CSS/XPath selectors and processors for each field.

When to Use

Use callbacks for:
  • E-commerce (products, prices, ratings)
  • Job boards (titles, companies, salaries)
  • Real estate (properties, prices, features)
  • Forums (posts, authors, replies)
  • Any non-article structured data
Use parse_article for:
  • News, blogs, documentation
  • Content with title/content/author/date structure

CallbackSchema

extract
object
required
Field extraction rules (mapping of field name → FieldExtractSchema). Min 1 field required.

FieldExtractSchema

css
string
default:"null"
CSS selector for extractionSyntax: "h1" (element), "h1::text" (text), "img::attr(src)" (attribute)
xpath
string
default:"null"
XPath expression for extraction
get_all
boolean
default:"false"
Extract all matches (returns list)
processors
ProcessorSchema[]
default:"null"
Value transformations applied sequentiallyAvailable: strip, replace, regex, cast, join, default, lowercase, parse_datetime

Nested Lists

type
string
default:"null"
Field type. Use "nested_list" for extracting lists of objects.
selector
string
default:"null"
CSS selector for nested list items. Required when type: "nested_list".
extract
object
default:"null"
Nested extraction config (field name → FieldExtractSchema). Required when type: "nested_list". Max depth: 3 levels.

ProcessorSchema

type
string
required
Processor type: strip, replace, regex, cast, join, default, lowercase, parse_datetime
Processor parameters:
  • replace: old, new (strings)
  • regex: pattern (string), group (int, default: 1)
  • cast: to (“int”|“float”|“bool”|“str”)
  • join: separator (string, default: ” ”)
  • default: default (any)
  • parse_datetime: format (string, optional)

Examples

E-commerce Product

Job Listing

Forum with Nested Comments

Field Storage

Standard fields map to database columns: url, title, content, author, published_date Custom fields stored in metadata_json column and flattened in exports

Reserved Names

Cannot use as callback names: parse_article, parse_start_url, start_requests, from_crawler, closed, parse

Validation

Callback names: Must be valid Python identifiers (e.g., parse_product, not parse-product) Field extraction: Must have css/xpath selector OR type: "nested_list" with selector and extract Cross-validation: Rules must reference defined callbacks

Workflow

  1. Analyze page: scrapai analyze page.html
  2. Test selectors: scrapai analyze page.html --test "h1.title"
  3. Build callback config with processors
  4. Import spider: scrapai spiders import spider.json --project proj
  5. Test extraction: scrapai crawl spider --limit 5 --project proj
  6. View results: scrapai show spider 1 --project proj