Skip to main content
Rules control which URLs are followed and how they are processed. Each rule defines URL patterns and optional callbacks for extraction.

SpiderRuleSchema

allow
string[]
default:"null"
Python regex patterns for URLs to allow (non-empty strings)Example:
deny
string[]
default:"null"
Python regex patterns for URLs to deny (takes precedence over allow)Example:
restrict_xpaths
string[]
default:"null"
Only follow links found in these XPath expressionsExample:
restrict_css
string[]
default:"null"
Only follow links found in these CSS selectorsExample:
callback
string
default:"null"
Callback function name for processing matched URLs. Must be valid Python identifier and defined in callbacks object.Built-in callbacks:
  • parse_article - Extract article content using configured extractors
Reserved names:
  • parse_article, parse_start_url, start_requests, from_crawler, closed, parse
Example:
Use null for navigation-only rules (follow links but don’t extract).
follow
boolean
default:"true"
Whether to follow links matching this ruleExample:
priority
integer
default:"0"
Rule priority (higher = processed first). Range: 0-1000Example:

Rule Matching

Allow/Deny Precedence

  1. If URL matches any deny pattern → rejected
  2. If URL matches any allow pattern → accepted
  3. If no allow patterns defined → accepted by default
  4. Otherwise → rejected

Restriction Scopes

  • restrict_xpaths and restrict_css limit where links are extracted from
  • Links outside these scopes are ignored, even if they match allow patterns

Examples

News Site

E-commerce

Forum/Discussion

Common Patterns

Exact Path Match

Exclude Query Parameters

Match Numeric IDs

Multiple Domains

Pagination

Rule Order

Rules are processed in array order. Use priority to control processing order within Scrapy.

Validation Errors

Undefined Callback

Fix: Add callback to callbacks object or use parse_article

Invalid Callback Name

Fix: Use underscores instead of hyphens: parse_product

Empty Patterns

Fix: Remove empty strings from allow/deny arrays