I have a Field() in my items.py called:
scores = Field()
I want multiple scrapers to append a value to a nested dict inside scores. For example, one of my scrapers:
item['scores']['baseball_score'] = '92'
And another scraper would:
item['scores']['basket_score'] = '21'
So that when I retrieve scores:
> item['scores']
{ 'baseball_score': '92', 'basket_score': '21' }
I do not want to initialize the dict inside my scraper because all my scrapers will be running simultaneously, so there could be race problems. Is there anyway for me to initialize item['scores'] as a nested dict in items.py? Or else, should I create a script before I run my scrapers to initialize it?
I actually want to make all of the fields in my Item as either a nested list or dict. Once my scrapers are done, I plan to aggregate these somehow in my pipelines.py.
This has got me thinking as well as to whether I should have a different Item Class for each of my scrapers, and then aggregate them into 1 item at the end once all the scrapers have finished. Thoughts?
Achievable using defaultdict
Then you can pass item to all your scrapers and they can each do add data at the appropriate key. Note that the above only creates a 2 level dict.