gpt4 book ai didi

python - 从 Pipeline 调用 Spider 的方法 - Python Scrapy

转载 作者:行者123 更新时间:2023-11-30 22:45:45 31 4
gpt4 key购买 nike

这是蜘蛛

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

我想从 spider_close() 方法调用 __set_last_start_date() 函数。但我收到此错误

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

最佳答案

类中以双下划线开头的名称旨在是私有(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.

请参阅this详细信息。

选项1.您可以重命名您的方法名称,而不是使用双下划线开头。

选项2。如果你想保留你的方法名称,那么用这种方式调用它,但我认为这不是一个好主意:

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

关于python - 从 Pipeline 调用 Spider 的方法 - Python Scrapy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41119576/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com