繁体   English   中英

使用 python 从 JSON 获取特定数据

[英]fetching specific data from JSON with python

我想从 JSON 中获取特定数据。 现在我可以获取所有数据并将其转换为 JSON 格式。 但我只想获取所有集合的“home_team”和“away_team”。

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

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'])

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

响应中的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'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM