【问题标题】:how to create list within a list until condition如何在列表中创建列表直到条件
【发布时间】:2022-12-24 22:24:17
【问题描述】:

我正在尝试抓取网站并尝试以特定格式返回数据: 我的代码:

for i in titles:                                                                                       
    title  = i.css('tr[class="bg_Sturm"] > td[class="hauptlink"]::text').get()                        
    if title is None:                                                                                                                                                                                                                                                                                                                                         
        try:                                                                                                                                                                                              
            date = i.css('tr > td[class="erfolg_table_saison zentriert"] ::text ').get(default = "")  
            club = i.css('tr > td[class="no-border-links"]>a ::text ').get(default = "").strip()                                                                                                        
            if date or club:                                                               
                print({date:club})                                                                                                                                                                          
        except (KeyError, AttributeError):                                                            
            pass                                                                                      
    else:                                                                                             
        print(title)   

我的输出:

2x Champions League participant
{'2021': 'Borussia Dortmund'}
{'2020': 'Red Bull Salzburg'}
1x German cup winner
{'20/21': 'Borussia Dortmund'}
2x Young player of the year
{'2020': ''}
{'2018': 'Eliteserien'}
1x German Bundesliga runner-up
{'19/20': 'Borussia Dortmund'}
3x Footballer of the Year
{'2021': 'Norway'}
{'2020': 'Norway'}
{'2019': 'Austria'}
2x Striker of the Year
{'21/22': 'Borussia Dortmund'}
{'20/21': 'Borussia Dortmund'}
1x Austrian cup winner
{'18/19': 'Red Bull Salzburg'}
3x Top scorer
{'20/21': 'UEFA Nations League B'}
{'20/21': 'UEFA Champions League'}
{'18/19': 'U-20 World Cup 2019'}
1x TM-Player of the season
{'2020': 'Austria'}

我想制作一份口述清单,以便在每个标题之后列出以下日期和俱乐部。看起来像这样:

[{"2x Champions League participant":[{"date": '2021':, "club": 'Borussia Dortmund', {"date":'2020':, "club": 'Red Bull Salzburg'}], 
{"1x German cup winner": [{"date":'20/21', "club": 'Borussia Dortmund'}],
"2x Young player of the year":[{"date":'2020', "club": ''},{"date":'2018', "club": 'Eliteserien'}]

等等...

【问题讨论】:

  • 声明result = [],然后在else块中声明current = []并附加result.append({title: current}),最后用current.append({date: club})替换你的print。
  • 我好像没跟我在哪里申报印刷品?你能告诉我完整的答案吗?

标签: python


【解决方案1】:

这是很简单的任务。从测试输出我假设标题总是出现在属于这个标题的数据之前,所以基本上你需要声明结果列表并附加字典 title 作为键和空列表作为值,一旦标题出现,然后将数据作为单独的字典添加到这个列表。

result = []
for i in titles:                                                                                       
    title  = i.css('tr[class="bg_Sturm"] > td[class="hauptlink"]::text').get()                        
    if title is None:                                                                                                                                                                                                                                                                                                                                         
        try:                                                                                                                                                                                              
            date = i.css('tr > td[class="erfolg_table_saison zentriert"] ::text ').get(default = "")  
            club = i.css('tr > td[class="no-border-links"]>a ::text ').get(default = "").strip()                                                                                                        
            if date or club:                                                               
                current.append({date: club})                                                                                                                                                                          
        except (KeyError, AttributeError):                                                            
            pass                                                                                      
    else:                                                                                             
        current = []
        result.append({title: current})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多