Search code examples
pythondictionaryscrapy

How to get a dict from scrapy item?


I need the values in a dict. But item uses some abstraction on top of it. How to get the fields in a dict from an item ?

I know scrapy allows dict to be returned in place of item now. But I already am using item in my code, so how to convert it.


Solution

  • It looks to me like :

    class Product(scrapy.Item):
        name = scrapy.Field()
    
    
    i = Product(name='foo)
    print dict(i)
    

    gets you a dictionary {'name': 'foo'}

    vars(p)
    p.__dict__
    

    gets you: {'_values': {'name': 'foo'}}

    If you don't want to create a new dictionary, just grab the _values key from the above:

    vars(p)['_values']
    p.__dict__['_values']