Spider class is an abstract base class for creating web spiders. It provides the core framework for asynchronous web crawling with support for pause/resume, session management, and flexible concurrency control.
Class Definition
Class Attributes
str | None
default:"None"
required
The name of the spider. Must be set in subclasses.
list[str]
default:"[]"
List of URLs where the spider will begin crawling. Used by default
start_requests() implementation.Set[str]
default:"set()"
Set of allowed domains. If set, only requests to these domains will be processed. Supports domain matching (e.g., “example.com” matches “sub.example.com”).
Concurrency Settings
int
default:"4"
Maximum number of concurrent requests globally.
int
default:"0"
Maximum number of concurrent requests per domain. If 0, only global limit applies.
float
default:"0.0"
Delay in seconds between requests to the same domain.
int
default:"3"
Maximum number of retry attempts for blocked requests.
Fingerprint Adjustments
bool
default:"False"
Include session kwargs in request fingerprinting for deduplication.
bool
default:"False"
Keep URL fragments when generating request fingerprints.
bool
default:"False"
Include headers in request fingerprinting.
Logging Settings
int
default:"logging.DEBUG"
Logging level for the spider logger.
str
Log message format.
{spider_name} will be replaced with the spider’s name.str
default:"%Y-%m-%d %H:%M:%S"
Date format for log messages.
str | None
default:"None"
Optional path to a log file. If set, logs will be written to this file.
Constructor
str | Path | AsyncPath | None
default:"None"
Directory for checkpoint files. If provided, enables pause/resume functionality.
float
default:"300.0"
Seconds between periodic checkpoint saves (default 5 minutes).
Abstract Methods
parse
Response
required
The response object to parse.
Methods
start_requests
start_urls. Override for custom initial request logic.
Yields: Request objects
Example:
start
bool
default:"False"
Whether to use the faster uvloop/winloop event loop implementation, if available.
Any
Asyncio backend options to pass to
anyio.run().CrawlResult object containing stats, items, and pause state
Example:
Pressing Ctrl+C initiates graceful shutdown. Pressing it again forces immediate stop.
If
crawldir is set, a checkpoint is saved on graceful shutdown for later resumption.stream
pause
RuntimeError if no active crawl is running
configure_sessions
start_requests() unless specified otherwise.
SessionManager
required
SessionManager instance to configure.
Hook Methods
These methods can be overridden to customize spider behavior:on_start
bool
default:"False"
True if the spider is resuming from a checkpoint.
on_close
on_error
Request
required
The request that caused the error.
Exception
required
The exception that was raised.
on_scraped_item
None to drop the item silently.
Dict[str, Any]
required
The scraped item to process.
is_blocked
Response
required
The response to check.
{401, 403, 407, 429, 444, 500, 502, 503, 504}
retry_blocked_request
Request
required
The request to retry (already copied with incremented retry count).
Response
required
The blocked response.
Properties
stats
stream() iteration).
Returns: CrawlStats object
Raises: RuntimeError if no active crawl is running
Complete Example
See Also
- Request - Creating and configuring requests
- CrawlerEngine - Understanding the crawl engine
- SessionManager - Managing multiple sessions
- CrawlResult - Working with crawl results