gpt4 book ai didi

python - 从 NBA API 收集特定球员信息

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:59 25 4
gpt4 key购买 nike

在此示例中,我希望使用 NBA API 收集特定数据,我试图从规范化字典中检索随机球员的全名和高度

from nba_api.stats.static import players
from nba_api.stats.endpoints import commonplayerinfo
import random

def activePlayers():
# Gets a random active player.
playerList = players.get_active_players()
random.shuffle(playerList)
playerList = playerList.pop()

# Gets random active players info
player_info = commonplayerinfo.CommonPlayerInfo(player_id=playerList['id'])
player_info = player_info.get_normalized_dict()
print('FULL_NAME: {}\nHEIGHT: {}'.format(player_info.get('DISPLAY_FIRST_LAST'), player_info.get('HEIGHT')))

activePlayers()

如果我将打印更改为 print(player_info) 这就是打印的内容:

{'CommonPlayerInfo': [{'PERSON_ID': 202339, 'FIRST_NAME': 'Eric', 'LAST_NAME': 'Bledsoe', 'DISPLAY_FIRST_LAST': 'Eric Bledsoe', 'DISPLAY_LAST_COMMA_FIRST': 'Bledsoe, Eric', 'DISPLAY_FI_LAST': 'E. Bledsoe', 'BIRTHDATE': '1989-12-09T00:00:00', 'SCHOOL': 'Kentucky', 'COUNTRY': 'USA', 'LAST_AFFILIATION': 'Kentucky/USA', 'HEIGHT': '6-1', 'WEIGHT': '205', 'SEASON_EXP': 9, 'JERSEY': '6', 'POSITION': 'Guard', 'ROSTERSTATUS': 'Active', 'TEAM_ID': 1610612749, 'TEAM_NAME': 'Bucks', 'TEAM_ABBREVIATION': 'MIL', 'TEAM_CODE': 'bucks', 'TEAM_CITY': 'Milwaukee', 'PLAYERCODE': 'eric_bledsoe', 'FROM_YEAR': 2010, 'TO_YEAR': 2019, 'DLEAGUE_FLAG': 'Y', 'NBA_FLAG': 'Y', 'GAMES_PLAYED_FLAG': 'Y', 'DRAFT_YEAR': '2010', 'DRAFT_ROUND': '1', 'DRAFT_NUMBER': '18'}], 'PlayerHeadlineStats': [{'PLAYER_ID': 202339, 'PLAYER_NAME': 'Eric Bledsoe', 'TimeFrame': '2018-19', 'PTS': 15.9, 'AST': 5.5, 'REB': 4.6, 'PIE': 0.125}], 'AvailableSeasons': [{'SEASON_ID': '12010'}, {'SEASON_ID': '22010'}, {'SEASON_ID': '22011'}, {'SEASON_ID': '42011'}, {'SEASON_ID': '12012'}, {'SEASON_ID': '22012'}, {'SEASON_ID': '42012'}, {'SEASON_ID': '12013'}, {'SEASON_ID': '22013'}, {'SEASON_ID': '12014'}, {'SEASON_ID': '22014'}, {'SEASON_ID': '12015'}, {'SEASON_ID': '22015'}, {'SEASON_ID': '12016'}, {'SEASON_ID': '22016'}, {'SEASON_ID': '12017'}, {'SEASON_ID': '22017'}, {'SEASON_ID': '42017'}, {'SEASON_ID': '12018'}, {'SEASON_ID': '22018'}, {'SEASON_ID': '42018'}]}

我得到的:

FULL_NAME: None
HEIGHT: None

我的期望:

FULL_NAME: Eric Bledsoe
HEIGHT: 6-1

最佳答案

你的 player_info 是一个有形状的字典

{
'CommonPlayerInfo': [ ... ],
'PlayerHeadlineStats': [ ... ],
'AvailableSeasons': [ ...]
}

您打算改用的字典是 'CommonPlayerInfo' 寻址列表的第一个元素。因此,如果您添加以下行

player_info = player_info.get('CommonPlayerInfo')[0]

print 语句之前,您应该得到您要查找的内容。

PS:在所有球员中,真的必须是埃里克布莱索吗?无论如何都不是我最喜欢的球员。

关于python - 从 NBA API 收集特定球员信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57229908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com