簡體   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