For the complete documentation index, see llms.txt. This page is also available as Markdown.

捕获网络请求(Fetch/XHR)

使用 XHR 标志从网页爬虫API获取网络活动。将页面加载期间的所有 Fetch/XHR 请求以结构化 JSON 形式返回,便于更容易提取数据。

有时,从浏览器在加载网页时发出的一个或多个 Fetch/XHR 请求中提取所需数据,比解析 HTML 更方便。

网页爬虫API 支持返回加载网页时发出的 XHR 请求列表。

用法

要在响应中获取 XHR 请求列表,请包含 "xhr": true 标志,如下面的载荷示例所示:

{
    "url": "https://example.com",
    "render": "html",
    "xhr": true
}

响应将包含一个 JSON 对象列表,每个对象代表一个单独的 XHR 请求。

{
    "results": [
        {
            "content": [
                    {
                    "url": "https://example.pilot.ebu.io/api/fallback/RTE",
                    "method": "GET",
                    "status_code": 200,
                    "response_body": "\"https://pilot-fallback.s3-eu-west-1.amazonaws.com/RTE.jpg\"",
                    "request_headers": {
                        "accept": "*/*",
                        "origin": "https://example.reco.ebu.io",
                        "referer": "https://example.com/",
                        "sec-ch-ua": "\"Chromium\";v=\"124\", \"Microsoft Edge\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
                        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
                        "sec-fetch-dest": "empty",
                        "sec-fetch-mode": "cors",
                        "sec-fetch-site": "cross-site",
                        "accept-encoding": "gzip, deflate, br, zstd",
                        "accept-language": "en-US,en;q=0.9",
                        "sec-ch-ua-mobile": "?0",
                        "sec-ch-ua-platform": "\"Windows\""
                        },
                    "request_payload": null,
                    "response_headers": {
                        "age": "491",
                        "via": "1.1 3ee81347c1935256691739f42090cfd8.cloudfront.net (CloudFront)",
                        "date": "Fri, 30 May 2025 05:30:44 GMT",
                        "x-eks": "1",
                        "server": "uvicorn",
                        "x-cache": "来自 cloudfront 的命中",
                        "x-amz-cf-id": "ESUATPCUwy-AJ-8OBgKoF3niz-jD87TIyyNypzOKNQL56VCL33XJGw==",
                        "content-type": "application/json",
                        "x-amz-cf-pop": "LAX54-P3",
                        "cache-control": "max-age=600",
                        "content-length": "59",
                        "access-control-allow-origin": "*"
                    }
                },
                {...更多 xhr 请求...}
            ],
            "type": "xhr"
}

输出键

说明

类型

url

XHR 请求的完整 URL

string

status_code

响应的 HTTP 状态码

整数

method

请求使用的 HTTP 方法

string

request_headers

键值对 请求 标头,包括 Cookie

对象

response_headers

键值对 响应 标头,包括 Cookie

对象

request_payload

载荷(仅对 POST、PUT、PATCH 请求包含此键)

string

response_body

响应正文

string

最后更新于

这有帮助吗?