尝试获取所有框分数时出现回溯错误
Traceback error when I am attempting to grab all box scores
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()
我收到错误 JSONDecodeError:期望值:第 1 行第 1 列(字符 0)错误。
我认为这与我的游戏 ID 在列表中有关?因为当我运行一个特定的游戏id号时,所以:
player_stats_data = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id='000127338', headers=headers, timeout=100)
stats_df = player_stats_data.get_data_frames()[0]
stats_df.head()
它returns那场比赛的数据。
为什么它没有读取列表中的所有游戏 ID?
编辑:
**这是我在收到答案后创建的 for 循环。这个 for 循环解决了我的问题。 **
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)
查看 API documentation,它需要一个游戏 ID。
一种方法是遍历您拥有的游戏 ID:
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...
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()
我收到错误 JSONDecodeError:期望值:第 1 行第 1 列(字符 0)错误。
我认为这与我的游戏 ID 在列表中有关?因为当我运行一个特定的游戏id号时,所以:
player_stats_data = boxscoreadvancedv2.BoxScoreAdvancedV2(game_id='000127338', headers=headers, timeout=100)
stats_df = player_stats_data.get_data_frames()[0]
stats_df.head()
它returns那场比赛的数据。
为什么它没有读取列表中的所有游戏 ID?
编辑:
**这是我在收到答案后创建的 for 循环。这个 for 循环解决了我的问题。 **
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)
查看 API documentation,它需要一个游戏 ID。
一种方法是遍历您拥有的游戏 ID:
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...