使用python从JSON获取特定数据

2024-05-12 19:27:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从JSON获取特定的数据。现在我可以获取所有数据并将其转换为JSON格式。但我只想把所有比赛的“主场队”和“客场队”都拿来

我获取所有数据的代码是:`

import json
import requests

reap = requests.get('https://app.sportdataapi.com/api/v1/soccer/matches?apikey=64572f90-88d6-11eb-8a43-7106c933e99d&season_id=496&date_from=2020-09-19&fbclid=IwAR2zkLfVHG1xyxCrKQdcvCqhYhyjp5vA2TtbAsXZ3R3pVFJKV3jnhgFdjG4')
data = reap.json()
print(data)

我的JSON数据是:

{'query': {'apikey': '64572f90-88d6-11eb-8a43-7106c933e99d', 'season_id': '496', 'date_from': '2020-09-19', 'fbclid': 'IwAR2zkLfVHG1xyxCrKQdcvCqhYhyjp5vA2TtbAsXZ3R3pVFJKV3jnhgFdjG4'}, 'data': [{'match_id': 139415, 'status_code': 3, 'status': 'finished', 'match_start': '2020-09-19 13:30:00', 'match_start_iso': '2020-09-19T13:30:00+00:00', 'minute': None, 'league_id': 314, 'season_id': 496, 'stage': {'stage_id': 1, 'name': 'Regular Season'}, 'group': {'group_id': 222, 'group_name': 'Bundesliga'}, 'round': {'round_id': 18564, 'name': '1', 'is_current': None}, 'referee_id': 433, 'home_team': {'team_id': 3993, 'name': '1. FC Union Berlin', 'short_code': 'UNI', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2819.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'away_team': {'team_id': 4075, 'name': 'FC Augsburg', 'short_code': 'FCA', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2814.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'stats': {'home_score': 1, 'away_score': 3, 'ht_score': '0-1', 'ft_score': '1-3', 'et_score': None, 'ps_score': None}, 'venue': {'venue_id': 1870, 'name': 'An der alten Forsterei', 'capacity': 22012, 'city': 'Berlin', 'country_id': 48}}, {'match_id': 139451, 'status_code': 3, 'status': 'finished', 'match_start': '2020-09-19 13:30:00', 'match_start_iso': '2020-09-19T13:30:00+00:00', 'minute': None, 'league_id': 314, 'season_id': 496, 'stage': {'stage_id': 1, 'name': 'Regular Season'}, 'group': {'group_id': 222, 'group_name': 'Bundesliga'}, 'round': {'round_id': 18564, 'name': '1', 'is_current': None}, 'referee_id': 466, 'home_team': {'team_id': 4070, 'name': 'Werder Bremen', 'short_code': 'SVW', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2810.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'away_team': {'team_id': 4067, 'name': 'Hertha BSC', 'short_code': 'BSC', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2806.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'stats': {'home_score': 1, 'away_score': 4, 'ht_score': '0-2', 'ft_score': '1-4', 'et_score': None, 'ps_score': None}, 'venue': {'venue_id': 1930, 'name': 'Weserstadion', 'capacity': 42100, 'city': 'Bremen', 'country_id': 48}}, {'match_id': 139479, 'status_code': 3, 'status': 'finished', 'match_start': '2020-09-19 13:30:00', 'match_start_iso': '2020-09-19T13:30:00+00:00', 'minute': None, 'league_id': 314, 'season_id': 496, 'stage': {'stage_id': 1, 'name': 'Regular Season'}, 'group': {'group_id': 222, 'group_name': 'Bundesliga'}, 'round': {'round_id': 18564, 'name': '1', 'is_current': None}, 'referee_id': 39, 'home_team': {'team_id': 3991, 'name': '1. FC Cologne', 'short_code': 'KOE', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2809.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 'away_team': {'team_id': 4079, 'name': 'TSG 1899 Hoffenheim', 'short_code': 'TSG', 'common_name': '', 'logo': 'https://cdn.sportdataapi.com/images/soccer/teams/100/2818.png', 'country': {'country_id': 48, 'name': 'Germany', 'country_code': 'de', 'continent': 'Europe'}}, 

现在如果我这样做:

print(data['data'])

它还显示了除查询和查询内部数据之外的所有数据


Tags: 数据namehttpscomnoneidmatchstatus
1条回答
网友
1楼 · 发布于 2024-05-12 19:27:48

data键在您的响应中,JSON是一个dict列表,您可以使用for循环和访问away_teamhome_team字典来迭代该列表

for d in data['data']:
    print(d['home_team'])
    
for d in data['data']:
    print(d['away_team'])

相关问题 更多 >