捕获网络请求(Fetch/XHR)
使用 XHR 标志从网页爬虫API 获取网络活动。以结构化 JSON 获取页面加载期间发出的所有 Fetch/XHR 请求,以便更轻松地提取数据。
有时,从浏览器在加载网页时发出的一个或多个 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": "Hit from 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": "*"
}
},
{...more xhr requests...}
],
"type": "xhr"
}输出键
描述
类型
url
XHR 请求的完整 URL
字符串
status_code
响应的 HTTP 状态码
整数
method
请求使用的 HTTP 方法
字符串
request_headers
键值对形式的 请求 标头,包括 Cookie
对象
response_headers
键值对形式的 响应 标头,包括 Cookie
对象
request_payload
载荷(仅在 POST、PUT、PATCH 请求中包含此键)
字符串
response_body
响应正文
字符串
最后更新于
这有帮助吗?

