Python 如何在解析函数中停止爬虫
我正在使用scrapy对搜索结果进行爬网。 我有一个变量Python 如何在解析函数中停止爬虫,python,scrapy,Python,Scrapy,我正在使用scrapy对搜索结果进行爬网。 我有一个变量search\u page,它告诉我们在哪个页码上 我在parse函数中有这个变量 现在我想知道,如果搜索页面>500那么爬虫程序应该停止爬行 我该怎么做 def parse(self, response): hxs = HtmlXPathSelector(response) sites = hxs.select('//div[@class="headline_area"]') items = [] fo
search\u page
,它告诉我们在哪个页码上
我在parse
函数中有这个变量
现在我想知道,如果搜索页面>500
那么爬虫程序应该停止爬行
我该怎么做
def parse(self, response):
hxs = HtmlXPathSelector(response)
sites = hxs.select('//div[@class="headline_area"]')
items = []
for site in sites[:5]:
item = StackItem()
log.msg(' LOOP' +str(ivar)+ '', level=log.ERROR)
item['title'] ="yoo ma"
request = Request("blabla", callback=self.test1)
request.meta['item'] = item
page_number = nextlink.split("&")[-3].split("=")[-1]
if page_number > 500:
STOP
ivar = ivar + 1
yield request
从scrapy.exceptions导入CloseSpider
if int(page_number) > 500:
raise CloseSpider('Search Exceeded 500')
从scrapy.exceptions导入CloseSpider
if int(page_number) > 500:
raise CloseSpider('Search Exceeded 500')
请发布相关代码。在
parse
函数的调用者中有一个变量。在每次解析调用中递增它。将停止
替换为中断
?请发布相关代码。在解析
函数的调用者中添加一个变量。在每次解析调用中递增它。将停止
替换为中断
?