欣赏某人可以帮助我理解规则如何堆叠深度爬行。堆叠多个规则会导致一次处理一个规则。目的是从MainPage获取链接,返回项目和响应,并将其传递给下一个将链接传递给另一个函数的规则,依此类推。
rules = {
Rule(LinkExtractor(restrict_xpaths=(--some xpath--)), callback='function_a', follow=True)
Rule(linkExtractor(restrict_xpaths=(--some xpath--)),callback='function_b', process_links='function_c', follow=True),
)
def function_a(self, response): --grab sports, games, link3 from main page--
item = ItemA()
i = response.xpath('---some xpath---')
for xpth in i:
item['name'] = xpth('---some xpath--')
yield item, scrapy.Request(url) // yield each item and url link from function_a back to the second rule
def function_b(self, response) -- receives responses from second rule--
//grab links same as function_a
def function_c(self, response) -- does process_links in the rule send the links it received to function_c?
这可以递归完成以深度抓取单个网站吗?我不确定我的规则概念是否正确。我是否必须添加X个规则来处理X深度页面,或者是否有更好的方法来处理递归深度爬网。
由于
答案 0 :(得分:1)
从This guide describes how to manage your policies开始,以下段落暗示每个规则都应用于每个页面。 (我的斜体)
规则
这是一个(或多个)规则对象的列表。每个规则定义一定的 抓取网站的行为。描述了规则对象 下面。 如果多个规则匹配相同的链接,则第一个规则将匹配 根据他们在此属性中定义的顺序使用。
在您的情况下,将每个规则定位到适当的页面,然后按深度顺序排列规则。