Selective link crawling
Restrict a CrawlSpider to URLs matching patterns such as product or article paths.
Scrapy · Class
Rule defines how a CrawlSpider extracts, processes, follows, and handles links. Configure it with a LinkExtractor plus optional callback, request/link processors, follow behavior, and errback.
Rule configures how CrawlSpider handles links found by a LinkExtractor. It can select links, dispatch matching responses to a callback, pass callback keyword arguments, transform links or requests, handle failures, and control whether subsequent links are followed.
scrapy.spiders.Rule(link_extractor: LinkExtractor | None = None, callback: CallbackT | str | None = None, cb_kwargs: dict[str, Any] | None = None, follow: bool | None = None, process_links: ProcessLinksT | str | None = None, process_request: ProcessRequestT | str | None = None, errback: Callable[[Failure], Any] | str | None = None)| Name | Type | Description |
|---|---|---|
link_extractor | LinkExtractor | None | LinkExtractor defining which links are extracted. |
callback | CallbackT | str | None | Callback callable or spider method name for extracted links. |
cb_kwargs | dict[str, Any] | None | Keyword arguments passed to the callback. |
follow | bool | None | Whether to follow links from matching responses. |
process_links | ProcessLinksT | str | None | Callable or method name processing extracted links. |
process_request | ProcessRequestT | str | None | Callable or method name processing generated requests. |
errback | Callable[[Failure], Any] | str | None | Callable or method name handling request failures. |
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class DocsSpider(CrawlSpider):
name = "docs"
allowed_domains = ["example.com"]
start_urls = ["https://example.com/"]
rules = (
Rule(LinkExtractor(allow=r"/articles/"), callback="parse_article", follow=True),
)
def parse_article(self, response):
yield {"url": response.url, "title": response.css("title::text").get()}
The rule limits extracted links to article paths, sends matching responses to parse_article, and continues following links found there.
Restrict a CrawlSpider to URLs matching patterns such as product or article paths.
Send matching pages to a named spider method that yields extracted items.
Apply custom filtering or normalization to extracted links before requests are created.
Customize generated requests or handle request failures for a rule.
Rule is a CrawlSpider configuration object that associates link extraction with optional callback, processing, error, and follow behavior. CrawlSpider applies the first matching rule when multiple rules match a link.
Follow-up: What happens if a generated request preserves another rule's metadata? It can cause the response to be dispatched to the wrong callback, so rule metadata must not be copied across rules.
link_extractor determines which links are extracted from each crawled page. Each extracted link becomes a Request, and its link text is stored in the request metadata under link_text.
Follow-up: How would you restrict crawling to product pages? Supply a LinkExtractor configured with an allow pattern for product URLs.
callback may be a callable or the name of a spider method and receives a Response. It should return an item, request, or iterable containing items and/or requests.
Follow-up: How does callback choice affect follow when follow is omitted? follow defaults to true without a callback and false when a callback is supplied.
CrawlSpiderLinkExtractorSpiderRequestResponse