I want to add item class within an item class

风流意气都作罢 提交于 2019-12-24 20:14:18

问题


Final JSON will be :

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

I want to create my items.py like (just example):

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()

How to implement this , already checked this https://doc.scrapy.org/en/latest/topics/items.html#extending-items

how to implement nested item in scrapy?

but there are no clue about this concept ... any suggestion?


回答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()

This way you can do this:

>>> 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()

Let me give you example,

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

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


来源:https://stackoverflow.com/questions/54248547/i-want-to-add-item-class-within-an-item-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!