Scrapy:ItemLoader,有人可以向我解释此错误吗?

时间:2018-12-08 20:24:18

标签: python scrapy

我有一个问题,我开始在BeautifulSoup上使用Scrapy,而我是Scrapy的新手,所以这个错误使我失望,我试图对其进行修复,但未成功。

我想使用Item Loader产生报废的物品,但出现错误,我不明白是什么问题。

有人可以向我解释我在做什么错。

这是我的蜘蛛代码quotes.py:

from scrapy import Spider
from scrapy.loader import ItemLoader
from quotes_spider.items import QuotesSpiderItem

class QuotesSpider(Spider):
    name = 'quotes'
    allowed_domains = ['http://quotes.toscrape.com']
    start_urls = ["http://quotes.toscrape.com"]


    def parse(self, response):
        l = ItemLoader(item=QuotesSpiderItem(), response=response)
        quotes = response.xpath('//*[@class="quote"]')
        for quote in quotes:
            text = quote.xpath('.//*[@class="text"]/text()').extract_first()
            author = quote.xpath('.//*[@itemprop="author"]/text()').extract_first()
            tags = quote.xpath('.//*[@class="tag"]/text()').extract_first ()

            l.add_value('Text', text)
            l.add_value('Author', author)
            l.add_value('Tags', tags)

            return l.load_item()

这是我的items.py文件:

import scrapy

class QuotesSpiderItem(scrapy.Item):
    text = scrapy.Field()
    author = scrapy.Field()
    tags = scrapy.Field()

这是我尝试运行该错误时遇到的错误:

Traceback (most recent call last):
  File "/home/djurovic/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/djurovic/Desktop/quotes_spider/quotes_spider/spiders/quotes.py", line 20, in parse
    l.add_value('Text', text)
  File "/home/djurovic/.local/lib/python3.6/site-packages/scrapy/loader/__init__.py", line 77, in add_value
    self._add_value(field_name, value)
  File "/home/djurovic/.local/lib/python3.6/site-packages/scrapy/loader/__init__.py", line 91, in _add_value
    processed_value = self._process_input_value(field_name, value)
  File "/home/djurovic/.local/lib/python3.6/site-packages/scrapy/loader/__init__.py", line 148, in _process_input_value
    proc = self.get_input_processor(field_name)
  File "/home/djurovic/.local/lib/python3.6/site-packages/scrapy/loader/__init__.py", line 137, in get_input_processor
    self.default_input_processor)
  File "/home/djurovic/.local/lib/python3.6/site-packages/scrapy/loader/__init__.py", line 154, in _get_item_field_attr
    value = self.item.fields[field_name].get(key, default)

1 个答案:

答案 0 :(得分:2)

您在Text中没有字段AuthorTagsQuotesSpiderItem,它们的名称分别是textauthor和{{1} }。字母大小写很重要。