2013-07-15 40 views
2

@Sjaak Trekhaak在這裏有一個'黑客'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問的,但沒有迴應。 謝謝,

回答

1

是你的蜘蛛的close_down屬性創建?因爲它看起來不像。

嘗試將您的支票更改爲if "close_down" in self.__dict__:或在您的蜘蛛的__init__()方法中添加self.close_down = False

+0

謝謝,結果我沒有正確初始化'close_down'屬性:我在'parse_item'方法中,它需要在類中。 –