这是蜘蛛
class TicketsSpider(scrapy.Spider):
def __set_last_start_date(self, dateString):
#code here
这是管道
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
我想从__set_last_start_date()
方法调用spider_closed()
函数。但是我收到了这个错误
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:
我可以向您保证,我可以spider.lastAdScrapedDate
,但我无法拨打spider.__set_last_start_date
答案 0 :(得分:3)
在类中,具有双下划线前导的名称是私有的。
__spam形式的任何标识符(至少两个前导下划线,最多一个尾随下划线)在文本上被_classname__spam替换,其中classname是当前的类名,其中前导下划线被剥离。
有关详细信息,请参阅this。
选项1.您可以重命名方法名称,而不是使用双下划线前导。
选项2.如果您想保留您的方法名称,请以这种方式调用它,但我认为这不是一个好主意:
def close_spider(self, spider):
spider._TicketsSpider__set_last_start_date(spider.lastAdScrapedDate)