使用管道时Scrapy KeyError

时间:2016-06-02 04:27:18

标签: python scrapy keyerror

有人能解释我为什么会收到此错误吗?我没理解。我尝试通过scrapy管道将我提取的数据存储到数据库中,它适用于另一个项目,但现在我得到以下错误:

Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 588, in _runCallbacks
        current.result = callback(current.result, *args, **kw)
      File "/home/Darknet/Darknet/pipelines.py", line 11, in process_item
        length = range(len(item['quantity']))
      File "/usr/lib/python2.7/dist-packages/scrapy/item.py", line 56, in __getitem__
        return self._values[key]
    KeyError: 'quantity'

我的管道:

import Darknet
from Darknet.items import DarknetItem
from models import db
from models import BlackMarket


class MysqlPipeline(object):

    def process_item(self, item, spider):

        length = range(len(item['quantity']))
        for x in length:
            record = BlackMarket(product=item['product'][x], quantity=item['quantity'][x], price=item['price'][x])
            db.add(record)
            db.commit()
        return item

我使用此代码段中的项目:

item = DarknetItem()
item['product'] = response.xpath('//div[@class="text oTitle"]/a/text()').extract()
item['price'] = response.xpath('//div[@class="bottom oPrice"]/text()').extract()
item['quantity'] = response.xpath('//div[@class="text oTitle"]/a/text()').extract()
yield item

0 个答案:

没有答案