Skip to content

Instantly share code, notes, and snippets.

@IvoVillanueva
Created July 3, 2022 16:28
Show Gist options
  • Save IvoVillanueva/84687124c769755975c92dbba25d6ae7 to your computer and use it in GitHub Desktop.
Save IvoVillanueva/84687124c769755975c92dbba25d6ae7 to your computer and use it in GitHub Desktop.
clutch
import pandas as pd
import requests
headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'es-ES,es;q=0.9,en;q=0.8',
'Connection': 'keep-alive',
'Host': 'stats.nba.com',
'If-Modified-Since': 'Fri, 01 Jul 2022 23:00:16 GMT',
'Origin': 'https://www.nba.com',
'Referer': 'https://www.nba.com/',
'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': "macOS",
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
'x-nba-stats-origin': 'stats',
'x-nba-stats-token': 'true'
}
def extract_season(season_list):
url_nba ="https://stats.nba.com/stats/leaguedashplayerclutch?AheadBehind=Ahead+or+Behind&ClutchTime=Last+5+Minutes&College=&Conference=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=PerGame&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&PointDiff=5&Rank=N&Season="+season_list+"&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight="
respuesta = requests.get(url_nba, headers=headers).json()
results = respuesta['resultSets'][0] ['rowSet']
df = pd.DataFrame(results)
df.columns = respuesta['resultSets'][0] ['headers']
df['year'] = season_list
df['year'] = Left = df['year'].str[:4].astype(int)+1
return df
season_list = (
'2017-18',
'2018-19',
'2019-20',
'2020-21',
'2021-22'
)
df = pd.concat(map(extract_season, season_list)).sort_values('year', ascending=False)
df.to_csv("cluttch3.csv", index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment