scrapyscrapy-shell

Scrapy KeyError(f"{self.__class__.__name__} does not support field: {key}"


Calling on all Scrapy experts to look into what this newbie missed.

I am getting the following error

KeyError(f"{self.__class__.__name__} does not support field: {key}"

My code is as follows:

In spiders/myCrawler.py

def parse_item(self, response):
    item = scrapy.Item()
    item['title'] = response.css('.p-card__info-title')
    return item

and in my settings.py

class MyItem(scrapy.Item):
    title = scrapy.Field()

I am unable to figure out what exactly I am doing wrong. Please help. Thank you.


Solution

  • You are using

    def parse_item(self, response):
        item = scrapy.Item()
        item['title'] = response.css('.p-card__info-title')
        return item
    

    In this you are initializing scrapy.Item() instead of your own item

    def parse_item(self, response):
        item = MyItem()
        item['title'] = response.css('.p-card__info-title')
        return item