Karim shoair commited on
Commit ·
ecc7bcb
1
Parent(s): b3032f2
docs(spiders): add a new page for `requests & responses`
Browse files- docs/spiders/requests-responses.md +202 -0
- zensical.toml +1 -0
docs/spiders/requests-responses.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Introduction
|
| 2 |
+
|
| 3 |
+
!!! success "Prerequisites"
|
| 4 |
+
|
| 5 |
+
1. You've read the [Getting started](getting-started.md) page and know how to create and run a basic spider.
|
| 6 |
+
|
| 7 |
+
This page covers the `Request` object in detail — how to construct requests, pass data between callbacks, control priority and deduplication, and use `response.follow()` for link-following.
|
| 8 |
+
|
| 9 |
+
## The Request Object
|
| 10 |
+
|
| 11 |
+
A `Request` represents a URL to be fetched. You create requests either directly or via `response.follow()`:
|
| 12 |
+
|
| 13 |
+
```python
|
| 14 |
+
from scrapling.spiders import Request
|
| 15 |
+
|
| 16 |
+
# Direct construction
|
| 17 |
+
request = Request(
|
| 18 |
+
"https://example.com/page",
|
| 19 |
+
callback=self.parse_page,
|
| 20 |
+
priority=5,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Via response.follow (preferred in callbacks)
|
| 24 |
+
request = response.follow("/page", callback=self.parse_page)
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
Here are all the arguments you can pass to `Request`:
|
| 28 |
+
|
| 29 |
+
| Argument | Type | Default | Description |
|
| 30 |
+
|---------------|------------|------------|-------------------------------------------------------------------------------------------------------|
|
| 31 |
+
| `url` | `str` | *required* | The URL to fetch |
|
| 32 |
+
| `sid` | `str` | `""` | Session ID — routes the request to a specific session (see [Sessions](sessions.md)) |
|
| 33 |
+
| `callback` | `callable` | `None` | Async generator method to process the response. Defaults to `parse()` |
|
| 34 |
+
| `priority` | `int` | `0` | Higher values are processed first |
|
| 35 |
+
| `dont_filter` | `bool` | `False` | If `True`, skip deduplication (allow duplicate requests) |
|
| 36 |
+
| `meta` | `dict` | `{}` | Arbitrary metadata passed through to the response |
|
| 37 |
+
| `**kwargs` | | | Additional keyword arguments passed to the session's fetch method (e.g., `headers`, `method`, `data`) |
|
| 38 |
+
|
| 39 |
+
Any extra keyword arguments are forwarded directly to the underlying session. For example, to make a POST request:
|
| 40 |
+
|
| 41 |
+
```python
|
| 42 |
+
yield Request(
|
| 43 |
+
"https://example.com/api",
|
| 44 |
+
method="POST",
|
| 45 |
+
data={"key": "value"},
|
| 46 |
+
callback=self.parse_result,
|
| 47 |
+
)
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
## Response.follow()
|
| 51 |
+
|
| 52 |
+
`response.follow()` is the recommended way to create follow-up requests inside callbacks. It offers several advantages over constructing `Request` objects directly:
|
| 53 |
+
|
| 54 |
+
- **Relative URLs** are resolved automatically against the current page URL
|
| 55 |
+
- **Referer header** is set to the current page URL by default
|
| 56 |
+
- **Session kwargs** from the original request are inherited (headers, proxy settings, etc.)
|
| 57 |
+
- **Callback, session ID, and priority** are inherited from the original request if not specified
|
| 58 |
+
|
| 59 |
+
```python
|
| 60 |
+
async def parse(self, response: Response):
|
| 61 |
+
# Minimal — inherits callback, sid, priority from current request
|
| 62 |
+
yield response.follow("/next-page")
|
| 63 |
+
|
| 64 |
+
# Override specific fields
|
| 65 |
+
yield response.follow(
|
| 66 |
+
"/product/123",
|
| 67 |
+
callback=self.parse_product,
|
| 68 |
+
priority=10,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Pass additional metadata to
|
| 72 |
+
yield response.follow(
|
| 73 |
+
"/details",
|
| 74 |
+
callback=self.parse_details,
|
| 75 |
+
meta={"category": "electronics"},
|
| 76 |
+
)
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
| Argument | Type | Default | Description |
|
| 80 |
+
|--------------------|------------|------------|------------------------------------------------------------|
|
| 81 |
+
| `url` | `str` | *required* | URL to follow (absolute or relative) |
|
| 82 |
+
| `sid` | `str` | `""` | Session ID (inherits from original request if empty) |
|
| 83 |
+
| `callback` | `callable` | `None` | Callback method (inherits from original request if `None`) |
|
| 84 |
+
| `priority` | `int` | `None` | Priority (inherits from original request if `None`) |
|
| 85 |
+
| `dont_filter` | `bool` | `False` | Skip deduplication |
|
| 86 |
+
| `meta` | `dict` | `None` | Metadata (merged with existing response meta) |
|
| 87 |
+
| **`referer_flow`** | `bool` | `True` | Set current URL as Referer header |
|
| 88 |
+
| `**kwargs` | | | Merged with original request's session kwargs |
|
| 89 |
+
|
| 90 |
+
### Disabling Referer Flow
|
| 91 |
+
|
| 92 |
+
By default, `response.follow()` sets the `Referer` header to the current page URL. To disable this:
|
| 93 |
+
|
| 94 |
+
```python
|
| 95 |
+
yield response.follow("/page", referer_flow=False)
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
## Callbacks
|
| 99 |
+
|
| 100 |
+
Callbacks are async generator methods on your spider that process responses. They must `yield` one of three types:
|
| 101 |
+
|
| 102 |
+
- **`dict`** — A scraped item, added to the results
|
| 103 |
+
- **`Request`** — A follow-up request, added to the queue
|
| 104 |
+
- **`None`** — Silently ignored
|
| 105 |
+
|
| 106 |
+
```python
|
| 107 |
+
class MySpider(Spider):
|
| 108 |
+
name = "my_spider"
|
| 109 |
+
start_urls = ["https://example.com"]
|
| 110 |
+
|
| 111 |
+
async def parse(self, response: Response):
|
| 112 |
+
# Yield items (dicts)
|
| 113 |
+
yield {"url": response.url, "title": response.css("title::text").get("")}
|
| 114 |
+
|
| 115 |
+
# Yield follow-up requests
|
| 116 |
+
for link in response.css("a::attr(href)").getall():
|
| 117 |
+
yield response.follow(link, callback=self.parse_page)
|
| 118 |
+
|
| 119 |
+
async def parse_page(self, response: Response):
|
| 120 |
+
yield {"content": response.css("article::text").get("")}
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
!!! tip "Note:"
|
| 124 |
+
|
| 125 |
+
All callback methods must be `async def` and use `yield` (not `return`). Even if a callback only yields items with no follow-up requests, it must still be an async generator.
|
| 126 |
+
|
| 127 |
+
## Request Priority
|
| 128 |
+
|
| 129 |
+
Requests with higher priority values are processed first. This is useful when some pages are more important to be processed first before others:
|
| 130 |
+
|
| 131 |
+
```python
|
| 132 |
+
async def parse(self, response: Response):
|
| 133 |
+
# High priority — process product pages first
|
| 134 |
+
for link in response.css("a.product::attr(href)").getall():
|
| 135 |
+
yield response.follow(link, callback=self.parse_product, priority=10)
|
| 136 |
+
|
| 137 |
+
# Low priority — pagination links processed after products
|
| 138 |
+
next_page = response.css("a.next::attr(href)").get()
|
| 139 |
+
if next_page:
|
| 140 |
+
yield response.follow(next_page, callback=self.parse, priority=0)
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
When using `response.follow()`, the priority is inherited from the original request unless you specify a new one.
|
| 144 |
+
|
| 145 |
+
## Deduplication
|
| 146 |
+
|
| 147 |
+
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.
|
| 148 |
+
|
| 149 |
+
To allow duplicate requests (e.g., re-visiting a page after login), set `dont_filter=True`:
|
| 150 |
+
|
| 151 |
+
```python
|
| 152 |
+
yield Request("https://example.com/dashboard", dont_filter=True, callback=self.parse_dashboard)
|
| 153 |
+
|
| 154 |
+
# Or with response.follow
|
| 155 |
+
yield response.follow("/dashboard", dont_filter=True, callback=self.parse_dashboard)
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
You can fine-tune what goes into the fingerprint using class attributes on your spider:
|
| 159 |
+
|
| 160 |
+
| Attribute | Default | Effect |
|
| 161 |
+
|----------------------|---------|----------------------------------------------------------------------------------------------------------------|
|
| 162 |
+
| `fp_include_kwargs` | `False` | Include extra request kwargs (arguments you passed to the session fetch, like headers, etc.) in the fingerprint |
|
| 163 |
+
| `fp_keep_fragments` | `False` | Keep URL fragments (`#section`) when computing fingerprints |
|
| 164 |
+
| `fp_include_headers` | `False` | Include request headers in the fingerprint |
|
| 165 |
+
|
| 166 |
+
For example, if you need to treat `https://example.com/page#section1` and `https://example.com/page#section2` as different URLs:
|
| 167 |
+
|
| 168 |
+
```python
|
| 169 |
+
class MySpider(Spider):
|
| 170 |
+
name = "my_spider"
|
| 171 |
+
fp_keep_fragments = True
|
| 172 |
+
# ...
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
## Request Meta
|
| 176 |
+
|
| 177 |
+
The `meta` dictionary lets you pass arbitrary data between callbacks. This is useful when you need context from one page to process another:
|
| 178 |
+
|
| 179 |
+
```python
|
| 180 |
+
async def parse(self, response: Response):
|
| 181 |
+
for product in response.css("div.product"):
|
| 182 |
+
category = product.css("span.category::text").get("")
|
| 183 |
+
link = product.css("a::attr(href)").get()
|
| 184 |
+
if link:
|
| 185 |
+
yield response.follow(
|
| 186 |
+
link,
|
| 187 |
+
callback=self.parse_product,
|
| 188 |
+
meta={"category": category},
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
async def parse_product(self, response: Response):
|
| 192 |
+
yield {
|
| 193 |
+
"name": response.css("h1::text").get(""),
|
| 194 |
+
"price": response.css(".price::text").get(""),
|
| 195 |
+
# Access meta from the request
|
| 196 |
+
"category": response.meta.get("category", ""),
|
| 197 |
+
}
|
| 198 |
+
```
|
| 199 |
+
|
| 200 |
+
When using `response.follow()`, the meta from the current response is merged with the new meta you provide (new values take precedence).
|
| 201 |
+
|
| 202 |
+
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.
|
zensical.toml
CHANGED
|
@@ -33,6 +33,7 @@ nav = [
|
|
| 33 |
{Spiders = [
|
| 34 |
{"Architecture" = "spiders/architecture.md"},
|
| 35 |
{"Getting started" = "spiders/getting-started.md"},
|
|
|
|
| 36 |
]},
|
| 37 |
{"Command Line Interface" = [
|
| 38 |
{Overview = "cli/overview.md"},
|
|
|
|
| 33 |
{Spiders = [
|
| 34 |
{"Architecture" = "spiders/architecture.md"},
|
| 35 |
{"Getting started" = "spiders/getting-started.md"},
|
| 36 |
+
{"Requests & Responses" = "spiders/requests-responses.md"},
|
| 37 |
]},
|
| 38 |
{"Command Line Interface" = [
|
| 39 |
{Overview = "cli/overview.md"},
|