1
我想讓scrapy管道工作以將數據導入我的數據庫。 我遇到的問題是無法存儲我的表中的主要字段的值是「contentid」。我使用的是可變current_affiliate_item但沒有任何運氣Pipeline Scrapy Python - 關鍵錯誤:'id'
在此先感謝您的幫助
File "/Users/pipelines.py", line 46, in _conditional_insert
self.update_affiliate_item(tx, item, affiliate_item)
File "/Users/pipelines.py", line 109, in update_affiliate_item
current_affiliate_item['id'], #used to be this - current_affiliate_item['id']
exceptions.KeyError: 'id'
這是它被調用的代碼! - 自我 - 與線46 LN 46開始。 update_affiliate_item(TX,項目,affiliate_item) LN 109 - current_affiliate_item [「身份證」]
我不知道我的問題是什麼在這裏,並已難倒它幾天我試圖打電話
if affiliate_item:
self.update_affiliate_item(tx, item, affiliate_item)
item_affiliate_id = affiliate_item['id']
item['affiliate_item_id'] = affiliate_item['id']
item['id'] = affiliate_item['item_id']
else:
item['id'] = self.get_item_id(tx, item)
item_affiliate_id = self.insert_affiliate_item(tx, item)
# item_affiliate_id = self.insert_or_update_affiliate(tx, item)
self.insert_or_update_photos(tx, item)
for price in item['prices']:
self.insert_or_update_price(tx, item, item_affiliate_id, price)
# log.msg("Item prices stored in db: %s" % item['name'], level=log.INFO)
def find_affiliate_item(self, tx, item): #changed selx to self
tx.execute("select * from z2ah5_jreviews_content where affiliate_item_id = %s and jr_retailer = %s", (item['affiliate_item_id'], item['retailer']))
result = tx.fetchone()
if result:
return result
else:
return False
def get_item_id(self, tx, item):
tx.execute("select * from z2ah5_jreviews_content where slug = %s ", (item['slug']))
result = tx.fetchone()
if result:
return result['contentid']
else:
tx.execute(\
"insert into z2ah5_jreviews_content (name, slug, date_created, date_modified) "
"values (%s, %s, %s, %s)",
(
item['name'],
item['slug'],
datetime.datetime.now(),
datetime.datetime.now(),
)
)
return tx.lastrowid
def insert_affiliate_item(self, tx, item):
tx.execute(\
"insert into z2ah5_jreviews_content (item_id, jr_retailer, affiliate_item_id, jr_description, date_created, date_modified) "
"values (%s, %s, %s, %s, %s, %s)",
(
item['id'],
item['retailer'],
item['affiliate_item_id'],
item['desc'],
datetime.datetime.now(),
datetime.datetime.now()
)
)
return tx.lastrowid
def update_affiliate_item(self, tx, item, current_affiliate_item): #used to be this - current_affiliate_item
tx.execute(\
"update z2ah5_jreviews_content set jr_description = %s, date_modified = %s where contentid = %s ",
(
item['desc'],
datetime.datetime.now(),
current_affiliate_item['id'], #used to be this - current_affiliate_item['id']
)
)
非常好。這似乎已經成功了。感謝您的幫助 – RunwithLuke 2012-07-23 16:34:52
if'id'in(affiliate_item or()): – 2012-07-24 14:14:25