Prerequisites
- You’ve read the Getting started page and know how to create and run a basic spider.
Request object in detail — how to construct requests, pass data between callbacks, control priority and deduplication, and use response.follow() for link-following.
The Request Object
ARequest represents a URL to be fetched. You create requests either directly or via response.follow():
Request Parameters
Here are all the arguments you can pass toRequest:
Any extra keyword arguments are forwarded directly to the underlying session. For example, to make a POST request:
Request Implementation
TheRequest class is defined in scrapling/spiders/request.py:
request.py:25-58
Response.follow()
response.follow() is the recommended way to create follow-up requests inside callbacks. It offers several advantages over constructing Request objects directly:
- Relative URLs are resolved automatically against the current page URL
- Referer header is set to the current page URL by default
- Session kwargs from the original request are inherited (headers, proxy settings, etc.)
- Callback, session ID, and priority are inherited from the original request if not specified
Response.follow() Parameters
Disabling Referer Flow
By default,response.follow() sets the Referer header to the current page URL. To disable this:
Callbacks
Callbacks are async generator methods on your spider that process responses. They mustyield one of three types:
dict— A scraped item, added to the resultsRequest— A follow-up request, added to the queueNone— Silently ignored
Request Priority
Requests with higher priority values are processed first. This is useful when some pages are more important to be processed first before others:response.follow(), the priority is inherited from the original request unless you specify a new one.
Deduplication
The spider automatically deduplicates requests based on a fingerprint computed from the URL, HTTP method, request body, and session ID. If two requests produce the same fingerprint, the second one is silently dropped.Fingerprint Generation
The fingerprint is generated inrequest.py:
request.py:64-113
Allowing Duplicates
To allow duplicate requests (e.g., re-visiting a page after login), setdont_filter=True:
Fine-tuning Fingerprints
You can fine-tune what goes into the fingerprint using class attributes on your spider:
For example, if you need to treat
https://example.com/page#section1 and https://example.com/page#section2 as different URLs:
Request Meta
Themeta dictionary lets you pass arbitrary data between callbacks. This is useful when you need context from one page to process another:
response.follow(), the meta from the current response is merged with the new meta you provide (new values take precedence).
The spider system also automatically stores some metadata. For example, the proxy used for a request is available as response.meta["proxy"] when proxy rotation is enabled.