Follow links with a callback
A spider needs to follow a discovered link and process the resulting page.
Scrapy · Class
scrapy.Request represents an HTTP request created by a spider and executed by Scrapy's Downloader. Configure its URL, method, callback, payload, metadata, headers, scheduling priority, and error handling.
scrapy.Request represents an HTTP request generated by a spider and executed by Scrapy's Downloader. Its options control the URL, HTTP method, callback or error callback, request data, metadata, priority, filtering, and related request behavior.
scrapy.Request(**args: Any, **kwargs: Any)| Name | Type | Description |
|---|---|---|
url | str | URL of the request; invalid URLs raise ValueError. |
callback | collections.abc.Callable | Callable invoked with the downloaded response. |
method | str | HTTP method; defaults to GET. |
meta | dict | Initial values for Request.meta; shallow-copied when supplied. |
body | bytes or str | Request body as bytes or string; strings are encoded using encoding. |
encoding | str | Request encoding, defaulting to utf-8. |
headers | dict-like | Request headers. |
cookies | dict or list | Request cookies. |
priority | int | Scheduling priority. |
dont_filter | bool | Whether duplicate-request filtering is disabled. |
errback | collections.abc.Callable | Callable used for request-processing errors. |
A scrapy.Request object representing the HTTP request; its execution produces a Response.
import scrapy
class ExampleSpider(scrapy.Spider):
name = "example"
def start_requests(self):
yield scrapy.Request(
"https://example.org/",
callback=self.parse,
headers={"Accept": "text/html"},
meta={"source": "homepage"},
)
def parse(self, response):
yield {"title": response.css("title::text").get()}Creates a GET request, routes its downloaded response to parse, and carries custom headers and metadata.
A spider needs to follow a discovered link and process the resulting page.
A request needs authentication, content negotiation, or custom HTTP behavior.
A callback needs context while processing a response.
A spider submits data or calls an endpoint other than the default GET request.
Request represents an HTTP request that a spider usually creates and the Downloader executes, producing a Response. The callback receives that downloaded response.
Follow-up: What happens when callback is omitted? Scrapy uses the spider's parse() method.
The callback handles a successful downloaded response, while errback handles errors raised during processing. The request can also carry headers, cookies, body data, metadata, and an encoding.
Follow-up: Why use meta? It passes request-scoped data through the crawl, but nested mutable values are not independently copied.
The method controls the HTTP method and defaults to GET. The URL is required, and invalid URLs raise ValueError.
Follow-up: How can request scheduling behavior be adjusted? Use priority and dont_filter, as documented request options.
scrapy.http.ResponseRequest.metaRequest.replaceRequest.copyscrapy.FormRequest