bigywater.tech
Scrapy · 43

request.meta 里的特殊键

request.meta 是 Scrapy 里最杂的一个字典:既有你写给 Scrapy 的开关,也有 Scrapy 写给你的记录。这篇从源码里把所有键列出来分类,再把前面没单独验证过的几个开关一一实测。

45 个
Scrapy 2.19 源码里读写的 request.meta 键总数(公开 40、内部 5)
24 / 10
其中你设置的输入键数 / Scrapy 写入的输出键数
含端口
HTTPAUTH_DOMAIN 匹配 URL 的 netloc,对带端口的 URL 必须写端口

输入键与输出键

实测 · 本机执行输出
  共 45 个键,其中公开 40 个、内部(下划线)5 个
  公开键:allow_offsite autothrottle_dont_adjust_delay bindaddress cache_timestamp cached_response cookiejar depth depth_reset dont_cache dont_merge_cookies dont_obey_robotstxt dont_redirect dont_retry download_latency download_maxsize download_slot download_timeout download_warnsize ftp_local_filename ftp_passive ftp_password ftp_user handle_httpstatus_all handle_httpstatus_list http_auth_domain http_pass http_user is_secure is_start_request max_retry_times priority_adjust proxy redirect_reasons redirect_times redirect_ttl redirect_urls referrer_policy retry_times rule verbatim_url
  内部键:_auth_proxy _callback _depth _dont_cache _scheme_proxy
  按用途分:你设置的输入键 24 个(allow_offsite autothrottle_dont_adjust_delay bindaddress cookiejar depth_reset dont_cache dont_merge_cookies dont_obey_robotstxt dont_redirect dont_retry download_maxsize download_slot download_timeout download_warnsize handle_httpstatus_all handle_httpstatus_list http_auth_domain http_pass http_user max_retry_times priority_adjust proxy referrer_policy verbatim_url);Scrapy 写入的输出键 10 个(cache_timestamp depth download_latency is_start_request redirect_reasons redirect_times redirect_ttl redirect_urls retry_times rule)
  源码里出现但上面两组都没列的公开键:cached_response ftp_local_filename ftp_passive ftp_password ftp_user is_secure

输入键(dont_redirect、download_timeout、proxy 等)改变这个请求怎么被处理;输出键(depth、redirect_*、retry_times、download_latency 等)是中间件写给你的记录。下划线开头的是内部键,别碰。

请求级的覆盖

实测 · 本机执行输出
  /big [('download_maxsize', 1000)]                           → errback DownloadCancelledError
  /big []                                                     → 回调 status=200 5000 字节
  /e404 [('handle_httpstatus_all', True)]                     → 回调 status=404 1 字节
  /e404 []                                                    → errback HttpError
  /e500 [('handle_httpstatus_list', [500])]                   → 回调 status=500 1 字节
  /e503 [('handle_httpstatus_list', [500])]                   → errback HttpError
  /slow [('download_timeout', 0.3)]                           → errback DownloadTimeoutError
  /slow []                                                    → 回调 status=200 18 字节

download_timeout、download_maxsize、handle_httpstatus_all/list 可以只对一个请求覆盖全局设置。

出口地址

实测 · 本机执行输出
  不设                                  → [{'who': 'callback'}];服务器看到的客户端地址 127.0.0.1
  ('127.0.0.1', 0)                    → [{'who': 'callback'}];服务器看到的客户端地址 127.0.0.1
  ('10.255.255.1', 0)(本机没有这个地址)       → [{'who': 'errback', 'exc': 'ConnectBindError', 'msg': "Couldn't bind: 49: Can't assign requested address."}];服务器看到的客户端地址 (没收到)

bindaddress 指定本地出口,本机没有的地址会得到 ConnectBindError。

Referer 策略

实测 · 本机执行输出
  None                              → 目标请求的 Referer = http://主机/link?q=1
  no-referrer                       → 目标请求的 Referer = (无)
  origin                            → 目标请求的 Referer = http://主机/
  same-origin                       → 目标请求的 Referer = http://主机/link?q=1
  strict-origin-when-cross-origin   → 目标请求的 Referer = http://主机/link?q=1
  unsafe-url                        → 目标请求的 Referer = http://主机/link?q=1

referrer_policy 决定跟随链接时发什么 Referer:不发、只发源、或完整 URL。

认证

实测 · 本机执行输出
  meta http_user/http_pass:/a → alice:s3cret;加 http_auth_domain=localhost(请求在 127.0.0.1)/b → (无);请求自带 Authorization 头 /c → ['Bearer own']
  只设 HTTPAUTH_USER / HTTPAUTH_PASS(没设 HTTPAUTH_DOMAIN):退出码≠0?是;服务器收到 0 个请求;错误:ValueError: HTTPAUTH_DOMAIN must be set when HTTPAUTH_USER or HTTPAUTH_PASS is configured. Set it to a domain (e.g. 'example.com') to restrict credent
  HTTPAUTH_DOMAIN=127.0.0.1(不带端口,请求 URL 带端口):/a → (无)
  HTTPAUTH_DOMAIN=localhost(请求在 127.0.0.1):/a → (无)
  HTTPAUTH_DOMAIN=127.0.0.1:<端口>(域名带上端口):/a → bob:pw  ← 匹配用的是 URL 的 netloc(含端口),所以 URL 里有端口时域名也得带端口
  meta http_auth_domain=127.0.0.1:<端口>:/b → alice:pw

http_user / http_pass 给单个请求加 Basic 认证,设置级的认证必须配域名;域名匹配的是含端口的 netloc。

最终有哪些键

实测 · 本机执行输出
  response.meta 共 10 个键:['download_latency', 'download_slot', 'download_timeout', 'is_start_request', 'mine', 'redirect_reasons', 'redirect_times', 'redirect_ttl', 'redirect_urls', 'retry_times']
  其中的取值:{'mine': 'x', 'is_start_request': True, 'download_timeout': '…', 'download_slot': '127.0.0.1', 'download_latency': '…', 'redirect_times': 1, 'redirect_ttl': 19, 'redirect_urls': '…', 'redirect_reasons': [302], 'retry_times': 1}

经过重定向、重试等中间件后,response.meta 里会多出一批输出键;它们不会被 response.follow 继承。

分步演示

request.meta 的键
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / Python 3.14.8 / Scrapy 2.19.0 / Twisted 26.4.0 / lxml 6.1.3 / parsel 1.12.1(临时虚拟环境);所有请求只发给本机自己起的测试服务器(127.0.0.1,端口 9960 以上,不访问任何真实网站);服务器可以按实验需要制造延迟、重定向、压缩、断连等行为,并记录收到的原始请求;每个实验在临时目录里新建 Scrapy 项目或单文件 spider,命令输出来自真实的 scrapy 进程;计时用 time.perf_counter,数字每次运行会有小幅抖动。第一节在虚拟环境里 grep Scrapy 源码(只做了字面匹配,键名可能漏掉用变量拼出来的);其余每项是单文件 spider 对本机服务器的实测
    • 没有验证:ftp_*、is_secure(FTP 处理器)的键;cache_timestamp、cached_response(HTTP 缓存内部);verbatim_url 的细节(指纹那一篇测过);给 give_up_log_level 设值的效果;其它第三方项目(如 scrapy-playwright)自己定义的 meta 键;键名的 grep 清单可能含个别误报或遗漏
    京ICP备2026067502号-1京公网安备11010502063487号