【发布时间】: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