我想要取消关于一些问题和答案的3深度网站。它有一个简单的结构如下所示:Scrapy在特定深度抓取简单的网站
- 第二深度 - >包含元数据(问题描述)
第三深度 - >包含实际数据(问题和答案)
/prob +-> /prob/problemLists.html +-> /prob/problem123456.html
我写了如下的Scrapy代码,使用response.meta['depth']
作为条件。
有没有更好的方法来做到这一点?
class DmzSpider(CrawlSpider):
rules = (
Rule(SgmlLinkExtractor(deny=('index\.htm',callback='parse_list'))),
)
def parse_list(self, response):
if response.meta['depth'] == 2:
# Scrap descriptions ...
return items
elif response.meta['depth'] ==3:
parse_item(response)
def parse_item(self, response):
# Parse items and save it according to prob_id...
return items
另外我曾尝试3个以下选项,其中没有人曾在总结request_depth_max = 1: 1.添加:从scrapy.conf导入设置 settings.overrides [ 'DEPTH_LIMIT'] = 2 蜘蛛文件 2.运行与-s选项命令行: 的/ usr /斌/ scrapy爬行-s DEPTH_LIMIT = 2 mininova.org 3.添加到settings.py中和scrapy.cfg: DEPTH_LIMIT = 2
它应该如何配置为超过1?
不知道这是你在找什么,但是:你可以使用它在默认情况下启用DepthLimitMiddleware设置的深度限制。有关其设置,请参阅:http://doc.scrapy.org/zh/latest/topics/spider-middleware.html#module-scrapy.contrib.spidermiddleware.depth –
我想要的是抓取第2和第3深度的页面。没有更深。我会编辑我的问题以使其更清楚。 –