アクティブなpostgreSQL接続を使用するために変数self.cursor
にアクセスしたいのですが、私はパイプラインクラスのスクラップのインスタンスにアクセスする方法を見つけることができません。アクセスのインスタンスは、パイプラインクラス
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'からアクセスできますか? – RottenCandy
objectNameは私には分かりません。スパイダーが自動的に起動するときにパイプラインクラスが呼び出されます。クラスのインスタンスにインスタンスをフックしたいのです! :) – atb00ker
おそらくあなたは 'getattr'を考慮する必要がありますhttps://stackoverflow.com/questions/4075190/what-is-getattr-exactly-and-how-do-i-use-it#4076099 – RottenCandy