我有一个简单的爬虫,它可以爬网网站上的所有链接。我需要根据命令行参数(例如boundary=3)对其进行限制。我的问题是我无法让CLOSESPIDER_ITEMCOUNT
工作。在settings.py中,我添加了EXTENSIONS = {'scrapy.extensions.closespider.CloseSpider': 1}
,但它仍然会抓取我的简单网站上的所有链接,而不是在1之后停止
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
import logging
import os
class FollowAllSpider(CrawlSpider):
custom_settings = {"CLOSESPIDER_ITEMCOUNT": 1, "CONCURRENT_REQUEST": 1}
name = 'follow_all'
allowed_domains = ['testdomain.com']
start_urls = ['https://www.testdomain.com/simple-website/']
rules = [Rule(LinkExtractor(), callback='parse_item', follow=True)]
def parse_item(self, response):
dirname = os.path.dirname(__file__)
filename = response.url.split("/")[-1] + '.html'
filePath = os.path.join(dirname, "pages/", filename)
with open(filePath, 'wb') as f:
f.write(response.body)
return
如果要限制爬网的页面数,应使用
CLOSESPIDER_PAGECOUNT
而不是CLOSESPIDER_ITEMCOUNT
还值得注意的是,您的spider不
yield
任何项目,因此如果要使用CLOSESPIDER_ITEMCOUNT
,则不需要计算任何项目,因为您直接在文件中写入通过单击链接,您可以阅读有关CLOSESPIDER_PAGECOUNT和CLOSESPIDER_ITEMCOUNT的更多信息
最后一件事,当使用
CLOSESPIDER_PAGECOUNT
时,您应该注意以下警告,因为您的结果可能与您的期望不符:https://stackoverflow.com/a/34535390/11326319相关问题 更多 >
编程相关推荐