【问题标题】:I want to add item class within an item class我想在项目类中添加项目类
【发布时间】:2019-06-12 09:33:18
【问题描述】:

最终的 JSON 将是:

            "address": ----,
            "state": ----,
            year: {
                "first": ----,
                "second": {
                    "basic": ----,
                    "Information": ----,
                    }
            },

我想创建我的 items.py 像(只是示例):

class Item(scrapy.Item): 
  address = scrapy.Field()
  state = scrapy.Field()
  year = scrapy.Field(first), scrapy.Field(second)

class first(scrapy.Item):
  amounts = scrapy.Field()

class second(scrapy.Item):
  basic = scrapy.Field()
  information = scrapy.Field()

这个怎么实现,已经查过了https://doc.scrapy.org/en/latest/topics/items.html#extending-items

how to implement nested item in scrapy?

但是没有关于这个概念的线索......有什么建议吗?

【问题讨论】:

    标签: python scrapy scrapy-spider


    【解决方案1】:
    class Item(scrapy.Item): 
      address = scrapy.Field()
      state = scrapy.Field()
      year = scrapy.Field(serializer=dict)
    
    class Year(scrapy.Item):
      first = scrapy.Field(serializer=dict)
      second = scrapy.Field(serializer=dict)
    
    class first(scrapy.Item):
      amounts = scrapy.Field()
    
    class second(scrapy.Item):
      basic = scrapy.Field()
      information = scrapy.Field()
    

    这样你就可以做到:

    >>> b = second(basic="hello", information="hello world")
    >>> a = first(amounts=3)
    >>> year = Year(first=a, second=b)
    >>> year
    {'first': {'amounts': 3},
     'second': {'basic': 'hello', 'information': 'hello world'}}
    >>> item = Item(address='address value', state='state value', year=year)
    >>> item
    {'address': 'address value',
     'state': 'state value',
     'year': {'first': {'amounts': 3}, 'second': {'basic': 'hello', 'information': 'hello world'}}}
    

    【讨论】:

      【解决方案2】:
      class Item(scrapy.Item): 
        address = scrapy.Field()
        state = scrapy.Field()
        year = scrapy.Field(first), scrapy.Field(second)    #You dont need to do like this
      
      class first(scrapy.Item):
        amounts = scrapy.Field()     #this work and below
      
      class second(scrapy.Item):    #and yes this work, you can do it in spider level or pipelines, just make your desired data, and pas it to year variable as you want. it will accumulate that
        basic = scrapy.Field()
        information = scrapy.Field()
      

      让我举个例子,

      first = {'first': first}
      second = {'basic': basic, 'info': info}
      
      year = {'first': first, 'second': second}
      

      【讨论】:

      • 谢谢@thundermind,在蜘蛛级别,我可以很容易地做到这一点,但我想在项目级别声明所有 json 元素,有什么概念吗?
      • 这里有 ItemLoadershttps://doc.scrapy.org/en/latest/topics/loaders.html,你也可以试试 pipelineshttps://doc.scrapy.org/en/latest/topics/item-pipeline.html
      猜你喜欢
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多