correct way to nest Item data in scrapy

前端 未结 2 933
故里飘歌
故里飘歌 2020-12-05 08:36

What is the correct way to nest Item data?

For example, I want the output of a product:

{
\'price\': price,
\'title\': title,
\'meta\': {
    \'url\'         


        
2条回答
  •  余生分开走
    2020-12-05 09:27

    I think it would be more straightforward to construct the dictionary in the spider. Here are two different ways of doing it, both achieving the same result. The only possible dealbreaker here is that the processors apply on the item['meta'] field, not on the item['meta']['added_on'] and item['meta']['url'] fields.

    def parse(self, response):
        item = MyItem()
        item['meta'] = {'added_on': response.css("a::text").extract()[0]}
        item['meta']['url'] = response.xpath("//a/@href").extract()[0]
        return item
    

    Is there a specific reason for which you want to construct it that way instead of unpacking the meta field ?

提交回复
热议问题