python - 在 Excel 中导入 CSV NBA 统计数据

标签 python json excel csv web-scraping

经过很长时间的努力,我找到了一种方法,以逗号分隔值的形式从 nba.com 获取数据

这是结果http://stats.nba.com/stats/leaguedashplayerstats?DateFrom=&DateTo=&GameScope=&GameSegment=&LastNGames=15&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2015-16&SeasonSegment=&SeasonType=Regular+Season&StarterBench=&VsConference=&VsDivision=

如何将其转换为漂亮的 CSV 或 Excel 文件? 或者如果可能的话更好的是,我如何像通过Excel Web查询Web查询表格一样自动查询这些数据?

最佳答案

以下内容应该可以帮助您入门:

import requests
import csv

url = "http://stats.nba.com/stats/leaguedashplayerstats?DateFrom=&DateTo=&GameScope=&GameSegment=&LastNGames=15&LeagueID=00&Location=&MeasureType=Advanced&Month=0&OpponentTeamID=0&Outcome=&PaceAdjust=N&PerMode=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2015-16&SeasonSegment=&SeasonType=Regular+Season&StarterBench=&VsConference=&VsDivision="

data = requests.get(url)
entries = data.json()

with open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(entries['resultSets'][0]['headers'])
    csv_output.writerows(entries['resultSets'][0]['rowSet'])

这将生成一个 output.csv 文件,开头如下:

PLAYER_ID,PLAYER_NAME,TEAM_ID,TEAM_ABBREVIATION,AGE,GP,W,L,W_PCT,MIN,OFF_RATING,DEF_RATING,NET_RATING,AST_PCT,AST_TO,AST_RATIO,OREB_PCT,DREB_PCT,REB_PCT,TM_TOV_PCT,EFG_PCT,TS_PCT,USG_PCT,PACE,PIE,FGM,FGA,FGM_PG,FGA_PG,FG_PCT,CFID,CFPARAMS
201166,Aaron Brooks,1610612741,CHI,31.0,13,6,7,0.462,17.5,105.8,106.8,-0.9,0.243,2.4,25.9,0.015,0.077,0.046,10.8,0.5,0.511,0.198,95.84,0.065,36,85,2.8,6.5,0.424,5,"201166,1610612741"
203932,Aaron Gordon,1610612753,ORL,20.0,15,3,12,0.2,23.0,98.9,106.4,-7.5,0.1,1.91,15.7,0.089,0.228,0.158,8.2,0.575,0.608,0.151,94.16,0.124,46,87,3.1,5.8,0.529,5,"203932,1610612753"
1626151,Aaron Harrison,1610612766,CHA,21.0,7,3,4,0.429,4.2,103.3,95.4,7.9,0.0,0.0,0.0,0.08,0.08,0.08,16.7,0.0,0.0,0.095,100.22,-0.032,0,5,0.0,0.7,0.0,5,"1626151,1610612766"

关于python - 在 Excel 中导入 CSV NBA 统计数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062372/

相关文章:

python - 合并两行数据并连接值(如果唯一)-Pandas

python - WxPython 框架未捕获 EVT_SET_FOCUS

jQuery:处理 getJSON() 中的错误?

Javascript嵌套树排名

regex - excel-vba正则表达式模式

python - 如何在 python 中创建矩阵计算类?

json - 使用 whereJsonContains 并将参数与字符串数组进行比较

Excel VBA - 将单元格中的日期与当前日期进行比较

excel - 分钟到小时

python - SQLAlchemy:单表继承,子项中的同一列