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

Pipeline Scrapy Python-键错误:';id';,python,scrapy,pipeline,Python,Scrapy,Pipeline,我正试图让这条杂乱无章的管道工作,将数据导入我的数据库。 我遇到的问题是无法为表中的主字段“contentid”存储值。我正在使用变量current\u affiliate\u item,但运气不好 提前感谢您的帮助 File "/Users/pipelines.py", line 46, in _conditional_insert self.update_affiliate_item(tx, item, affiliate_item) File "/Users/pipelines

我正试图让这条杂乱无章的管道工作,将数据导入我的数据库。 我遇到的问题是无法为表中的主字段“contentid”存储值。我正在使用变量current\u affiliate\u 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行开始 LN46为自我更新附属项目(发送、项目、附属项目) ln 109是-当前附属项目['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']
            )               
        )

错误的原因是,当您调用
update\u affiliate\u item
函数时,dict
affiliate\u item
没有“id”键

而不是这个

if affiliate_item:
试试这个:

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

非常好。这似乎成功了。如果(附属项目或())中的“id”,感谢您的帮助: