我想将爬虫作为广度优先搜索来运行。
所以,我写了下面的代码。
from scrapy.spider import BaseSpider
from scrapy.http import Request
class MySpider(BaseSpider):
start_urls = ['http://example.com/a']
def parse(self, response):
next_a = response.css('.next::attr(href)').extract_first()
if next_a:
yield Request(next_a, callback=self.parse, priority = 3)
for b in response.css('.b::attr("href")'):
yield Request(b, callback=self.parse_b, priority = 2)
def parse_b(self, response):
pass
Run Code Online (Sandbox Code Playgroud)
我希望这个爬虫能按如下方式移动。
a1, a2, a3, ..., an, b1, b2, b3, ...., bn
但实际上会是这样的。
a1, b1, b2, ...., b_n1, a2, b_n1+1, b_n1+2, ....
怎样才能按预期运行呢?
| 归档时间: |
|
| 查看次数: |
1349 次 |
| 最近记录: |