简体   繁体   English

从Pipeline - Python Scrapy调用Spider的方法

[英]Call a Spider's method from Pipeline - Python Scrapy

This is Spider 这是蜘蛛

class TicketsSpider(scrapy.Spider):

    def __set_last_start_date(self, dateString):
        #code here

This is Pipeline 这是管道

class TicketsPipeline(object):

    def spider_closed(self, spider):
        spider.__set_last_start_date(spider.lastAdScrapedDate)

    @classmethod
    def from_crawler(cls, crawler):
        pipeline = cls()
        crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
        return pipeline

I want to call __set_last_start_date() function from spider_closed() method. 我想从spider_closed()方法调用__set_last_start_date()函数。 But I am getting this error 但是我收到了这个错误

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 149, in maybeDeferred
    result = f(*args, **kw)
  File "build/bdist.linux-x86_64/egg/pydispatch/robustapply.py", line 55, in robustApply
  File "tickets/pipelines.py", line 236, in spider_closed
    spider.__set_last_start_date(spider.lastAdScrapedDate)
AttributeError: 'TicketsSpider' object has no attribute '_TicketsPipeline__set_last_start_date'
2016-12-13 02:49:53 [scrapy] INFO: Dumping Scrapy stats:

I can assure you that I can get spider.lastAdScrapedDate but I am unable to call spider.__set_last_start_date 我可以向你保证,我可以获得spider.lastAdScrapedDate但我无法调用spider.__set_last_start_date

Names, in a class, with double underscore leading are intended to be private. 具有双下划线前导的类中的名称是私有的。

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. 形式__spam的任何标识符(至少两个前导下划线,最多一个尾随下划线)在文本上用_classname__spam替换,其中classname是当前类名,其中前导下划线被剥离。

Please see this for detail. 请参阅详细。

option 1. You can rename your method name, not use double underscore leading. 选项1.您可以重命名方法名称,而不是使用双下划线前导。

option 2. If you want to keep your method name, then call it in this way, but I don't think it's a good idea: 选项2.如果你想保留你的方法名称,然后以这种方式调用它,但我认为这不是一个好主意:

    def close_spider(self, spider):
        spider._TicketsSpider__set_last_start_date(spider.lastAdScrapedDate)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM