Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 802
user568109 Avatar asked Aug 06 '15 12:08

user568109


1 Answers

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']
like image 137
Robert Moskal Avatar answered Oct 09 '22 10:10

Robert Moskal