Pipeline Scrapy Python - 关键错误:'id'

时间:2012-07-23 15:59:59

标签: python scrapy pipeline

我正在努力让scrapy管道工作以将数据汇集到我的数据库中。 我遇到的问题是无法为我的表中的主要字段存储“contentid”的值。我正在使用变量current_affiliate_item,但没有运气。

提前感谢您的帮助!

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行开始 46是 - self.update_affiliate_item(tx,item,affiliate_item) 109是 - current_affiliate_item ['id']

我不知道我的问题是什么,并且已经被困了好几天了。我正试图打电话

        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']
            )               
        )

1 个答案:

答案 0 :(得分:2)

错误是由于当你调用affiliate_item函数时,dict update_affiliate_item没有'id'键这一事实

而不是

if affiliate_item:

试试这个:

if affiliate_item and affiliate_item.has_key('id'):