【发布时间】:2017-12-03 06:34:03
【问题描述】:
我想访问变量self.cursor 以利用活动的postgreSQL 连接,但我无法弄清楚如何访问scrapy 的管道类实例。
class ScrapenewsPipeline(object):
def open_spider(self, spider):
self.connection = psycopg2.connect(
host= os.environ['HOST_NAME'],
user=os.environ['USERNAME'],
database=os.environ['DATABASE_NAME'],
password=os.environ['PASSWORD'])
self.cursor = self.connection.cursor()
self.connection.set_session(autocommit=True)
def close_spider(self, spider):
self.cursor.close()
self.connection.close()
def process_item(self, item, spider):
print ("Some Magic Happens Here")
def checkUrlExist(self, item):
print("I want to call this function from my spider to access the
self.cursor variable")
请注意,我意识到我可以使用yield item 访问process_item,但该功能正在做其他事情,我希望通过checkUrlExist 中的self.cursor 访问连接并能够调用实例随意从我的蜘蛛那里上课!
谢谢。
【问题讨论】:
-
objectName.cursor? -
objectName 我不知道,当蜘蛛自动启动时会调用管道类,我想将一个实例挂钩到该类的实例! :)
-
也许你应该考虑
getattrstackoverflow.com/questions/4075190/…
标签: python-3.x scrapy scrapy-spider scrapy-pipeline