只能使用 commonteamroster 端点拉取 2020 团队花名册

Can only pull 2020 team roster using commonteamroster endpoint

我目前正在尝试使用 commonteamroster 端点从 NBA 获取最新的球队名单,但似乎 season_id 我输入的任何内容,都只是 returns 2020 年的名单。

我正在寻找的花名册可以在这里找到: https://www.nba.com/stats/team/1610612738/?Season=2021-22

我的代码

import pandas as pd
import requests
import time

headers  = {
        'Connection': 'keep-alive',
        'Accept': 'application/json, text/plain, */*',
        'x-nba-stats-token': 'true',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
        'x-nba-stats-origin': 'stats',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Referer': 'https://stats.nba.com/',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'en-US,en;q=0.9',
    }

from nba_api.stats.endpoints import commonteamroster


# get teams from the reg season 2021-22
teamfinder = commonteamroster.CommonTeamRoster(season='2021-22',
                                              team_id='1610612738',
                                              league_id_nullable='00')
teams = teamfinder.get_data_frames()[0]

team_id = teams['TeamID'].unique().tolist()
team_ids = teams['TeamID'].unique().tolist()

teams_roster=[]

for team_id in team_ids:
    player_stats_data = commonteamroster.CommonTeamRoster(team_id=team_id, headers=headers, timeout=100)
    time.sleep(.600)
    df = player_stats_data.common_team_roster.get_data_frame()
    df['TeamID'] = team_id
    print(team_id)
    teams_roster.append(df)

这个returns:

[        TeamID SEASON LeagueID               PLAYER NICKNAME  
 0   1610612738   2020       00         Jayson Tatum   Jayson   
 1   1610612738   2020       00       Carsen Edwards   Carsen   
 2   1610612738   2020       00         Jaylen Brown   Jaylen   
 3   1610612738   2020       00          Moses Brown    Moses   
 4   1610612738   2020       00     Payton Pritchard   Payton   
 5   1610612738   2020       00       Grant Williams    Grant   
 6   1610612738   2020       00     Tristan Thompson  Tristan   
 7   1610612738   2020       00        Jabari Parker   Jabari   
 8   1610612738   2020       00        Aaron Nesmith    Aaron   
 9   1610612738   2020       00         Marcus Smart   Marcus   
 10  1610612738   2020       00         Semi Ojeleye     Semi   
 11  1610612738   2020       00          Luke Kornet     Luke   
 12  1610612738   2020       00           Al Horford       Al   
 13  1610612738   2020       00  Robert Williams III   Robert   
 14  1610612738   2020       00       Romeo Langford    Romeo   
 15  1610612738   2020       00       Tremont Waters  Tremont   
 16  1610612738   2020       00        Evan Fournier     Evan   
 17  1610612738   2020       00           Tacko Fall    Tacko   

无论我设置什么赛季编号,它只会拉出 2020 赛季名单?

它工作正常。你没看输出吗:

# get teams from the reg season 2021-22
teamfinder = commonteamroster.CommonTeamRoster(season='2021-22',
                                              team_id='1610612738',
                                              league_id_nullable='00')

默认情况下它抓取 2020 赛季。您忽略了在 for 循环中添加该季节参数。

for team_id in team_ids:
    player_stats_data = commonteamroster.CommonTeamRoster(season='2021-22', team_id=team_id, headers=headers, timeout=100)   # <--- ADD YOUR season parameter!
    time.sleep(.600)
    df = player_stats_data.common_team_roster.get_data_frame()
    df['TeamID'] = team_id
    print(team_id)
    teams_roster.append(df)