我想访问该变量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")
请注意,我意识到我可以process_item
通过 using访问,但该函数正在做其他事情,我希望通过inyield item
访问连接并能够随意从我的蜘蛛调用类的实例!谢谢你。self.cursor
checkUrlExist