-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_standings.py
131 lines (118 loc) · 3.16 KB
/
get_standings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Import Libs
import requests
import pandas as pd
def get_standings():
seasons =input('Enter the seasons you would like to get data from separated by space (ex:"2020-21 2019-20"): ')
season_list = seasons.split()
headers = {
'Connection': 'keep-alive',
'Accept': 'application/json, text/plain, */*',
'x-nba-stats-token': 'true',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
'x-nba-stats-origin': 'stats',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Referer': 'https://stats.nba.com/',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
}
# Column names from stats.nba.com
columns_list_standings = [
"LeagueID",
"SeasonID",
"TeamID",
"TeamCity",
"TeamName",
"TeamSlug",
"Conference",
"ConferenceRecord",
"PlayoffRank",
"ClinchIndicator",
"Division",
"DivisionRecord",
"DivisionRank",
"WINS",
"LOSSES",
"WinPCT",
"LeagueRank",
"Record",
"HOME",
"ROAD",
"L10",
"Last10Home",
"Last10Road",
"OT",
"ThreePTSOrLess",
"TenPTSOrMore",
"LongHomeStreak",
"strLongHomeStreak",
"LongRoadStreak",
"strLongRoadStreak",
"LongWinStreak",
"LongLossStreak",
"CurrentHomeStreak",
"strCurrentHomeStreak",
"CurrentRoadStreak",
"strCurrentRoadStreak",
"CurrentStreak",
"strCurrentStreak",
"ConferenceGamesBack",
"DivisionGamesBack",
"ClinchedConferenceTitle",
"ClinchedDivisionTitle",
"ClinchedPlayoffBirth",
"ClinchedPlayIn",
"EliminatedConference",
"EliminatedDivision",
"AheadAtHalf",
"BehindAtHalf",
"TiedAtHalf",
"AheadAtThird",
"BehindAtThird",
"TiedAtThird",
"Score100PTS",
"OppScore100PTS",
"OppOver500",
"LeadInFGPCT",
"LeadInReb",
"FewerTurnovers",
"PointsPG",
"OppPointsPG",
"DiffPointsPG",
"vsEast",
"vsAtlantic",
"vsCentral",
"vsSoutheast",
"vsWest",
"vsNorthwest",
"vsPacific",
"vsSouthwest",
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
]
# List of season_ids
# season_list = ['2020-21', '2019-20', '2018-19', '2017-18', '2016-17']
dfs=[]
for season_id in season_list:
standings_info_url = 'https://stats.nba.com/stats/leaguestandingsv3?LeagueID=00&Season='+season_id+'&SeasonType=Regular%20Season'
response = requests.get(url=standings_info_url, headers=headers).json()
standings_info = response['resultSets'][0]['rowSet']
df = pd.DataFrame(standings_info, columns=columns_list_standings)
df['season_id'] =season_id
print(season_id)
dfs.append(df)
# Save DataFrame to an excel file
final_df = pd.concat(dfs, sort=False)
final_df.to_excel('standings.xlsx')
if __name__ == '__main__':
get_standings()