【问题标题】:How to stop a spider from pipeline?如何阻止蜘蛛进入管道?
【发布时间】:2013-07-15 04:23:25
【问题描述】:

@Sjaak Trekhaak 在这里有一个“hack”How do I stop all spiders and the engine immediately after a condition in a pipeline is met?,它可以通过在管道中设置一个标志来阻止蜘蛛,然后在解析器方法中调用 CloseSpider。但是我在管道中有以下代码(其中 pdate 和 lastseen 是明确定义的日期时间):

class StopSpiderPipeline(object):
    def process_item(self, item, spider):                                       
        if pdate < lastseen:
            spider.close_down = True 

在蜘蛛中

def parse_item(self, response):                                             
    if self.close_down:                                                     
        raise CloseSpider(reason='Already scraped')     

我收到错误exceptions.AttributeError: 'SyncSpider' object has no attribute 'close_down',我哪里出错了?这个问题实际上是由@anicake 提出的,但没有得到回应。 谢谢,

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    你的蜘蛛的close_down 属性创建了吗?因为它看起来不像。

    尝试将您的支票更改为if "close_down" in self.__dict__: 或在您的蜘蛛的__init__() 方法中添加self.close_down = False

    【讨论】:

    • 谢谢,原来我没有正确初始化close_down 属性:我在parse_item 方法中做了,它需要在类中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多