Scrapy with a nested array

I am new to scrapy and would like to understand how to clear an object for output in nested JSON. Right now I am creating JSON that looks like

[
{'a' : 1, 
'b' : '2',
'c' : 3},
]

And I will like it more:

[
{ 'a' : '1',
'_junk' : [
     'b' : 2,
     'c' : 3]},
]

--- where I put some things in the subfield _junkfor later processing later.

I have the current code in the parser definition file scrapername.py...

item['a'] = x
item['b'] = y
item['c'] = z

And it looks like

item['a'] = x
item['_junk']['b'] = y
item['_junk']['c'] = z

--- can fix this, but I get an error in _junk:

  File "/usr/local/lib/python2.7/dist-packages/scrapy/item.py", line 49, in __getitem__
    return self._values[key]
exceptions.KeyError: '_junk'

Does this mean that I need to somehow change mine items.py? I currently have:

class Website(Item):
    a = Field()
    _junk = Field()
    b = Field()
    c = Field()
+5
source share
1 answer

You need to create a spam dictionary before storing items in it.

item['a'] = x
item['_junk'] = {}
item['_junk']['b'] = y
item['_junk']['c'] = z
+7
source

All Articles