Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在解析函数中停止爬虫_Python_Scrapy - Fatal编程技术网

Python 如何在解析函数中停止爬虫

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

我正在使用scrapy对搜索结果进行爬网。 我有一个变量
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
函数的调用者中有一个变量。在每次解析调用中递增它。将
停止
替换为
中断
?请发布相关代码。在
解析
函数的调用者中添加一个变量。在每次解析调用中递增它。将
停止
替换为
中断