Skip to main content
 首页 » 编程设计

python之如何阻止管道中的蜘蛛

2025年05月04日8kerrycode

@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 提出的,但没有得到回应。 谢谢,

请您参考如下方法:

您的蜘蛛的close_down 属性是否已创建?因为它看起来不像。

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