Reputation: 23
import pandas as pd
import requests
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 leaguegamefinder
# get game logs from the reg season
gamefinder = leaguegamefinder.LeagueGameFinder(season_nullable='2020-21',
league_id_nullable='00',
season_type_nullable='Regular Season')
games = gamefinder.get_data_frames()[0]
# get a list of distinct game ids
game_id = games['GAME_ID'].unique().tolist()
player_stats_data = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id=game_id, headers=headers, timeout=100)
stats_df = player_stats_data.get_data_frames()[0]
stats_df.head()
I get error JSONDecodeError: Expecting value: line 1 column 1 (char 0) error.
I believe it has something to do with my game id's being in a list ? Because when I run a specfic game id number, so:
player_stats_data = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id='000127338', headers=headers, timeout=100)
stats_df = player_stats_data.get_data_frames()[0]
stats_df.head()
It returns the data from that game.
Why is it not reading all game id's in the list though?
EDIT:
**Here is the for loop I have created after receiving an answer. This for loop has solved my issue. **
for game_id in game_ids:
player_stats_data = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id=game_id, headers=headers, timeout=100)
time.sleep(.600)
df = player_stats_data.player_stats.get_data_frame()
df['GAME_ID'] = game_id
print(game_id)
boxscores.append(df)
Upvotes: 1
Views: 483
Reputation: 311913
Looking at the API documentation, it takes a single game id.
One approach could be to loop over the game ids you have:
game_ids = games['GAME_ID'].unique().tolist()
for game_id in game_ids:
player_stats_data = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id=game_id, headers=headers, timeout=100)
# Use the stats...
Upvotes: 1