Reputation: 23
I'm currently attempting to grab the latest team rosters from the NBA using the commonteamroster endpoint, but it seems like whatever season_id I enter, it only returns the 2020 rosters.
The roster I am looking at grabbing can be found here: https://www.nba.com/stats/team/1610612738/?Season=2021-22
My code
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)
This 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
It doesn't matter what I set my season number, it only pulls 2020 season roster?
Upvotes: 0
Views: 609
Reputation: 28630
It works fine. Did you not look at the output from:
# get teams from the reg season 2021-22
teamfinder = commonteamroster.CommonTeamRoster(season='2021-22',
team_id='1610612738',
league_id_nullable='00')
By default it grabs 2020 season. You neglected to add that season parameter in your for loop.
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)
Upvotes: 1