通过抓取网站生成数据的篮球参考客户端
basketball-reference-web-scraper的Python项目详细描述
篮球参考刮板
Basketball Reference是一个很好的网站(特别是对于像我这样的篮球数据狂来说),希望他们不会因为我创建了这个网站而对我太生气。
基本上,我创建了这个存储库,作为另一个项目的实用工具,我试图估计一个nba球员的生产力,因为它与日常幻想运动有关。对于那个项目,我需要方块分数和调度信息,这是由这个实用程序提供的。
这是PyPi package。
通过PIP安装
我编写这个库是为了创建第一个PyPi
包。
希望这意味着如果您想使用这个库,您可以通过pip这样简单地下载包
pip install basketball_reference_web_scraper
这个库需要Python 3.4+
,并且只支持1999-2000
季节之后的季节
客户机
您可以像这样导入client
# This imports the clientfrombasketball_reference_web_scraperimportclient
在data
模块中还定义了一些有用的enum
s,它们可以像import
那样^{/p>
# This imports the Team enumfrombasketball_reference_web_scraper.dataimportTeam
API
此客户端有五种方法
- 按日期获取玩家框分数(
client.player_box_scores
) - 按日期获取团队框分数(
client.team_box_scores
) - 获取一个季节的日程安排(
client.season_schedule
) - 获取一个赛季的球员总数(
client.players_season_totals
) - 获取球员一个赛季的高级赛季统计数据(
client.players_advanced_season_totals
)
您可以看到在this ^{
数据输出
此客户端还支持三种输出类型:
- python数据类型(即a
list
或结果) JSON
CSV
此客户端的版本>=3
将CSV
输出到指定的文件路径,并返回JSON
输出或将其写入指定的文件路径。
- 通过将
output_type
值设置为OutputType.JSON
或OutputType.CSV
来指定输出类型。- 客户端方法的默认返回值是
Python
数据结构(方法返回box_scores
的list
的dict
)
- 客户端方法的默认返回值是
- 如果希望将输出输出输出到特定文件,请设置
output_file_path
变量-对于CSV
输出,必须定义此变量 - 指定
output_write_option
指定如何将输出写入指定文件(OutputWriteOption.WRITE
对应于w
)- 默认写入选项是
OutputWriteOption.WRITE
- 默认写入选项是
数据解析
- 一些数据片段,如玩家的团队或游戏的结果被解析为枚举(例如,前两个示例中分别是
Team
和Outcome
枚举) - 当输出到
JSON
或CSV
时,这些枚举被序列化为字符串,但是当处理Python
数据结构时,您将看到这些枚举值。- 例如,希望这些枚举使
client
用户更容易实现团队特定的逻辑。
- 例如,希望这些枚举使
按日期获取玩家盒子分数
frombasketball_reference_web_scraperimportclientfrombasketball_reference_web_scraper.dataimportOutputType# Get all player box scores for January 1st, 2017 client.player_box_scores(day=1,month=1,year=2017)# Get all player box scores for January 1st, 2017 in JSON formatclient.player_box_scores(day=1,month=1,year=2017,output_type=OutputType.JSON)# Output all player box scores for January 1st, 2017 in JSON format to 1_1_2017_box_scores.jsonclient.player_box_scores(day=1,month=1,year=2017,output_type=OutputType.JSON,output_file_path="./1_1_2017_box_scores.json")# Output all player box scores for January 1st, 2017 in JSON format to 1_1_2017_box_scores.csvclient.player_box_scores(day=1,month=1,year=2017,output_type=OutputType.CSV,output_file_path="./1_1_2017_box_scores.csv")
按日期获取团队框分数
frombasketball_reference_web_scraperimportclient# Get all team totals for January 1st, 2018client.team_box_scores(day=1,month=1,year=2018)# The team_box_scores method also supports all output behavior previously described
获取季节计划
frombasketball_reference_web_scraperimportclient# Get all games for the 2017-2018 seasonclient.season_schedule(season_end_year=2018)# The schedule method also supports all output behavior previously described
获取所有玩家的赛季总数
frombasketball_reference_web_scraperimportclient# Get 2017-2018 season totals for all playersclient.players_season_totals(season_end_year=2018)# The players_season_totals method also supports all output behavior previously described
获取所有玩家的高级赛季统计信息
frombasketball_reference_web_scraperimportclient# Get 2017-2018 advanced season statistics for all playersclient.players_advanced_season_totals(season_end_year=2018)# The players_advanced_season_totals method also supports all output behavior previously described
获取游戏的逐场播放数据
api的结构是由于basketball reference在获取逐局数据时具有独特的url模式, 这取决于比赛日期和主队。
示例:https://www.basketball-reference.com/boxscores/pbp/201810160BOS.html
frombasketball_reference_web_scraperimportclientfrombasketball_reference_web_scraper.dataimportTeam# Get play-by-play data for 2018-10-16 game played at the Boston Celticsplay_by_play=client.play_by_play(home_team=Team.BOSTON_CELTICS,year=2018,month=10,day=16,)
开发
目前支持的主要版本有两个-V3
和V4
。
有两个分支,v3
和v4
对于这两个主要版本-它们实际上是要使用的“主”分支
做出改变时。
master
将反映最新的主要版本分支。
贡献者
感谢他们的贡献!