python 从stats.nba.com中删除NBA球员统计数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从stats.nba.com中删除NBA球员统计数据相关的知识,希望对你有一定的参考价值。

import requests
import csv
import sys

# get me all active players

url_allPlayers = ("http://stats.nba.com/stats/commonallplayers?IsOnlyCurrentSeason"
		"=0&LeagueID=00&Season=2015-16")

#request url and parse the JSON
response = requests.get(url_allPlayers)
response.raise_for_status()
players = response.json()['resultSets'][0]['rowSet']

# use roster status flag to check if player is still actively playing
active_players = [players[i] for i in range(0,len(players)) if players[i][2]==1 ]	

ids = [active_players[i][0] for i in range(0,len(active_players))]

print("Number of Active Players: " + str(len(ids)))

name_height_pos = []

for i in ids:

	url_onePlayer=("http://stats.nba.com/stats/commonplayerinfo?"
		"LeagueID=00&PlayerID=" + str(i) + "&SeasonType=Regular+Season")
	#request url and parse the JSON
	response = requests.get(url_onePlayer)
	response.raise_for_status()
	one_player = response.json()['resultSets'][0]['rowSet']
	stats_player = response.json()['resultSets'][1]['rowSet']

	try:
		points = stats_player[0][3]
		assists = stats_player[0][4]
		rebounds = stats_player[0][5]
		PIE = stats_player[0][6]
	# handle the case, where player is active, but didn't play
	# in any game so far in this season (-1 just a place holder value)
	except IndexError:
		points = -1
		assists = -1
		rebounds = -1
		PIE = -1

	name_height_pos.append([one_player[0][1] + " " + one_player[0][2], 
		one_player[0][10], 
		one_player[0][14], 
		one_player[0][18],
		"http://i.cdn.turner.com/nba/nba/.element/img/2.0/sect/statscube/players/large/"+one_player[0][1].lower()+"_"+ one_player[0][2].lower() +".png",
		points,
		assists,
		rebounds,
		PIE])

#convert from inches to cm
for i in range(0,len(name_height_pos)):
	feet = name_height_pos[i][1][0]
	inches = name_height_pos[i][1][2:]

	result = (float(feet) * 12 + float(inches)) * 2.54;
	name_height_pos[i][1] = result



answer = input("Save as player.csv -- y/n\t")
if answer[0] == "n":
	sys.exit()
elif answer[0] != "y":
	sys.exit()
else:

	with open("player.csv", "w") as csvfile:
		writer = csv.writer(csvfile, delimiter=",", lineterminator="\n")
		writer.writerow(["Name","Height","Pos","Team","img_Link","PTS","AST","REB","PIE"])
		for row in name_height_pos:
			writer.writerow(row)
		print("Saved as \'player.csv\'")

以上是关于python 从stats.nba.com中删除NBA球员统计数据的主要内容,如果未能解决你的问题,请参考以下文章

从 stats.nba.com 抓取数据,curl::curl_fetch_memory(url, handle = handle) 中出现错误

如何使用 Python 创建一个 NBA 得分图

stats.nba.com 的 CORS/CORB

Python的请求库超时但从浏览器获得响应

NBA Stats JSON 解析 Swift

如何从python的scrapy输出中删除'\ n'