我想访问变量self.cursor
以利用活动的postgreSQL连接,但我无法弄清楚如何访问scrapy的管道类实例。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
访问,但该功能是做其他的东西,我想通过self.cursor
在checkUrlExist
连接的访问,并能够从调用类的实例我蜘蛛随意! 谢谢。
'objectName.cursor'? – RottenCandy
objectName在我不知道的时候,管道类会在蜘蛛自动启动时调用,我想将一个实例挂接到该类的实例上! :) – atb00ker
也许你应该考虑'getattr' https://stackoverflow.com/questions/4075190/what-is-getattr-exactly-and-how-do-i-use-it#4076099 – RottenCandy