how to implement nested item in scrapy?

后端 未结 2 1307
鱼传尺愫
鱼传尺愫 2020-12-08 10:43

I am scraping some data with complex hierarchical info and need to export the result to json.

I defined the items as

class FamilyItem():
    name =         


        
相关标签:
2条回答
  • 2020-12-08 10:56

    Not sure if there's a way to do nested items in scrapy with classes but arrays work fine. You could do something like this:

    grandson = Grandson(name = 'Grandson', age = 2)
    
    son = Son(name = 'Son', grandsons = [grandson])
    
    item = Item(name = 'Name', son = [son])
    
    0 讨论(0)
  • 2020-12-08 11:00

    When saving the nested items, make sure to wrap them in a call to dict(), e.g.:

    gs1 = GrandsonsItem()
    gs1['name'] = 'GS1'
    gs1['age'] = 18
    gs1['weight'] = 50
    
    gs2 = GrandsonsItem()
    gs2['name'] = 'GS2'
    gs2['age'] = 19
    gs2['weight'] = 51
    
    s1 = SonsItem()
    s1['name'] = 'S1'
    s1['grandsons'] = [dict(gs1), dict(gs2)]
    
    jenny = FamilyItem()
    jenny['name'] = 'Jenny'
    jenny['sons'] = [dict(s1)]
    
    0 讨论(0)
提交回复
热议问题