首页 > 解决方案 > sportsreference API 错误数据问题

问题描述

我正在使用sportsreference API 来获取一些数据,但我不确定我做错了什么或者API 有问题。当我使用 API 提取我需要的数据时,它总是说客队赢了,即使在比赛中也不是这样。

代码片段:

from sportsreference.nba.boxscore import Boxscores
from sportsreference.nba.boxscore import Boxscore

# Select range of dates to get boxscores from (year, month, day)
games = Boxscores(datetime(2017, 10, 17), datetime(2017, 10, 20))

# Get boxscore abbreviations to get more detailed game boxscores
boxscore_abvs = []
for key in games.games.keys():
    for i in range(len(games.games[key])):
        boxscore_abvs.append(games.games[key][i]['boxscore'])

# Get more detailed boxscores
df = pd.DataFrame()
for abv in boxscore_abvs:
    game_data = Boxscore(abv)
    temp_df = game_data.dataframe
    df = df.append(temp_df)

df 的错误输出示例(骑士队赢得了这场比赛,API 报告凯尔特人队):

              away_assist_percentage  away_assists  away_block_percentage  away_blocks  away_defensive_rating  ...            losing_name   pace  winner  winning_abbr           winning_name
201710170CLE                    66.7            24                    6.6            4                  102.7  ...    Cleveland Cavaliers   99.3    Away           BOS         Boston Celtics

标签: pythonpython-3.xdataframe

解决方案


这是一个由站点更改其 HTML 布局引起的已知问题。似乎它应该在0.6.0版本中修复:https ://github.com/roclark/sportsreference/pull/506 。

同时,您可以从 git 安装以获取固定版本:

pip install --force-reinstall git+https://github.com/roclark/sportsreference.git@master

这样,我得到了正确的结果:

Boxscore('201710170CLE').dataframe[['away_points', 'home_points', 'winning_name']]
#               away_points  home_points         winning_name
# 201710170CLE           99          102  Cleveland Cavaliers

推荐阅读