DEPTH_LIMIT 到底从哪里开始数?DEPTH_PRIORITY 为什么能改成广度优先?重定向、起始请求、跨域名的 depth 怎么算?这篇在一个二叉树站点上把层号序列和请求数量都量出来。
DEPTH_LIMIT 服务器收到 request_depth_max 各层请求数(层 0、1、2…)
0 63 5 [1, 2, 4, 8, 16, 32]
1 3 1 [1, 2]
2 7 2 [1, 2, 4]
3 15 3 [1, 2, 4, 8]
4 31 4 [1, 2, 4, 8, 16]
DEPTH_LIMIT=2 的日志:[scrapy.spidermiddlewares.depth] DEBUG: Ignoring link (depth > 2): http://127.0.0.1:9960/t
response.meta["depth"] 与页面所在层(DEPTH_LIMIT=2):t0:0, t2:1, t1:1, t6:2, t4:2, t3:2, t5:2 …DepthMiddleware 给每个从响应产生的请求设置 meta["depth"] = 响应深度 + 1,超过 DEPTH_LIMIT 就丢掉。起始请求深度为 0,所以 DEPTH_LIMIT = N 会抓到第 N 层。
默认(DEPTH_PRIORITY = 0) 011223333223333 (共 15 个页面,每个字符是该页面所在层) DEPTH_PRIORITY = 1 011222233333333 (共 15 个页面,每个字符是该页面所在层) DEPTH_PRIORITY = 1 + FIFO 队列 011222233333333 (共 15 个页面,每个字符是该页面所在层) DEPTH_PRIORITY = -1(深度越深优先级越高) 011223333223333 (共 15 个页面,每个字符是该页面所在层)
请求优先级减去 深度 × DEPTH_PRIORITY。为正数时浅层优先(广度优先),为 0 时按调度器默认顺序(后进先出,深度优先)。
leaf response.meta["depth"] = 1(redirect_reasons=[302]) t0 response.meta["depth"] = 0 t1 response.meta["depth"] = 1 t2 response.meta["depth"] = 0 t3 response.meta["depth"] = 1 服务器收到:['/t0', '/t3', '/t2', '/red', '/leaf', '/t1']
重定向沿用原请求的 depth;depth_reset 让请求的深度归零;回调里手动写 meta 的 depth 会被覆盖。
t0: depth = 3 t1: depth = 4 同样的代码,DEPTH_LIMIT=3:回调被调用的页面 ['t0'](子请求深度 4 > 3 被丢弃)
start() 里的请求自带 depth 时会保留,子请求从这个值 +1 数起。
不加 depth_reset:回调被调用 [('1', '/t0', 0), ('l', '/t1', 1)](h=127.0.0.1/localhost 首字母)
跨域链接加 depth_reset:回调被调用 [('1', '/t0', 0), ('l', '/t1', 0), ('l', '/t3', 1)](h=127.0.0.1/localhost 首字母)depth 不区分域名,要让每个域名自己从 0 数,给跨域链接加 depth_reset。