5

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.

user568109
  • 47,225
  • 17
  • 99
  • 123
  • Have a look at [http://stackoverflow.com/questions/21945067/how-list-all-fields-of-an-class-in-python-and-no-methods][1] [1]: http://stackoverflow.com/questions/21945067/how-list-all-fields-of-an-class-in-python-and-no-methods – PenguMC Aug 06 '15 at 12:54

1 Answers1

8

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']
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86