nba_api是否有只查询api的一部分的方法?

问题描述

我目前正在为我的网站编写一个模拟程序,并且目前对API感到非常满意,但是,当试图收集某个团队在某项特定游戏中的得分和得分时,我可以发现确实是winprobabilityPBP。但是,为了进行模拟,我需要查询一个季节中每场比赛的API,并且从API返回的数据太大,因为它还包含游戏中每场比赛的详细信息。有没有一种请求API的方法,但它仅返回列表之一,而不是每次播放的详细信息以及较小的得分统计。由于查询花费的时间太长而无法使用。谢谢

PS。这是我的代码,向您展示我的意思

from nba_api.stats.static import players,teams
from nba_api.stats.endpoints import teamgamelog,winprobabilitypbp
import random as rnd
import numpy as np
import matplotlib.pyplot as plt

#get team ids
team1 = teams.find_teams_by_full_name("Bucks")
team2 = teams.find_teams_by_full_name("Lakers")
team1_id = team1[0]['id']
team2_id = team2[0]['id']

#get all games in specified season for those teams
results1 = teamgamelog.TeamGameLog(team_id=team1_id,season='2019-20').get_dict()
results2 = teamgamelog.TeamGameLog(team_id=team2_id,season='2019-20').get_dict()
games1 = results1['resultSets'][0]['rowSet']
games2 = results2['resultSets'][0]['rowSet']

#create lists with game ids
game_id1 = []
for i in range(len(games1)): 
    game_id1.append(games1[i][1]) 

game_id2 = []
for i in range(len(games2)):
    game_id2.append(games2[i][1])
    

#query API for points scored by team and against team,store those in separate lists (these requests take too long as it returns details on each play in each game of which there is normally 82 games!)
team_points1 = []
opp_points1 = []
for id in game_id1:
    game1_scores = winprobabilitypbp.WinProbabilityPBP(game_id=id,timeout=1000).get_dict()['resultSets'][1]['rowSet']
    if game1_scores[0][3] == team1[0]['abbreviation']:
        team_points1.append(game1_scores[0][4])
        opp_points1.append(game1_scores[0][7])
    else:
        team_points1.append(game1_scores[0][7])
        opp_points1.append(game1_scores[0][4])


#same for other team
team_points2 = []
opp_points2 = []
for id in game_id2:
    game2_scores = winprobabilitypbp.WinProbabilityPBP(game_id=id,timeout=1000).get_dict()['resultSets'][1]['rowSet']
    if game2_scores[0][3] == team2[0]['abbreviation']:
        team_points2.append(game2_scores[0][4])
        opp_points2.append(game2_scores[0][7])
    else:
        team_points2.append(game2_scores[0][7])
        opp_points2.append(game2_scores[0][4])

#use numpy to find mean and standard deviations for each list collected
team1_mean = np.mean(team_points1)
team1_std = np.std(team_points1)

opp1_mean = np.mean(opp_points1)
opp1_std = np.std(opp_points1)

team2_mean = np.mean(team_points2)
team2_std = np.std(team_points2)

opp2_mean = np.mean(opp_points2)
opp2_std = np.std(opp_points2)

#use mean and standard deviations with random to simulate number of points a team will score
team1_sim_pts = int(round((rnd.gauss(team1_mean,team1_std) + rnd.gauss(opp2_mean,opp2_std))) / 2)

team2_sim_pts = int(round((rnd.gauss(team2_mean,team2_std) + rnd.gauss(opp1_mean,opp1_std))) / 2)

#print the scores
print(f"Bucks: {team1_sim_pts}")

print(f"Lakers: {team2_sim_pts}")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)