19

I want to add a field to scrapy.Item so that it's an array:

class MyItem(scrapy.Item):
    field1 = scrapy.Field()
    field2 = scrapy.Field()
    field3_array = ???

How can I do that?

Mario Honse
  • 289
  • 1
  • 3
  • 10

1 Answers1

27

You just create a filed

field3_array = scrapy.Field()

But while parsing the scraped items do like this

items['field3_array'] = []

items['field3_array'][0] ='one'
items['field3_array'][1] ='two'

in this way you can achieve this.

Have a look

Community
  • 1
  • 1
backtrack
  • 7,996
  • 5
  • 52
  • 99
  • 3
    Will this create a dict or a list? From scrapy documentation of Field, I suspect dict. In that case, it would be less misleading with `items['field3_array'] = {}`. – amit kumar May 06 '15 at 16:14
  • For empty list `items['field3_array'][0] = ...` will inevitably give IndexError, you should use `append` to add items. – cl0ne Dec 30 '20 at 02:37