我想訪問變量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