Using Python to scrape NBA Individual Player Stats in less than 20 lines

Jed Ong
2 min readNov 26, 2020

I’m in the process of getting back to analyzing and visualizing NBA stats (Other Basketball Stats). The most time consuming effort so far was for me to figure out the best way to scrape the NBA Stats API to the get the data I need. With that, here’s the quickest way I’ve found so far to scrape Individual Player Season stats:

import pandas as pd
import requests
url = """
https://stats.nba.com/stats/leaguedashplayerstats?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=Totals&Period=0&PlayerExperience=&PlayerPosition=&PlusMinus=N&Rank=N&Season=2019-20&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&StarterBench=&TeamID=0&TwoWay=0&VsConference=&VsDivision=&Weight=
"""
header_dict = {
'User-Agent': 'Mozilla/5.0',
'x-nba-stats-origin': 'stats',
'x-nba-stats-token': 'true',
'Referer': 'https://stats.nba.com',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Host': 'stats.nba.com'
}
res = requests.get(url, headers=header_dict)
json_set = res.json()
headers = json_set['resultSets'][0]['headers']
data_set = json_set['resultSets'][0]['rowSet']
df = pd.DataFrame(data_set, columns=headers)

..and that’s it! Here’s what the resulting Pandas DataFrame looks like:

Some key things about the code:

  • url: Each stats type or category is served by its own API URL; the above example is specific for individual player season stats for the 2019–2020 NBA Season (see the Season parameter in the URL).
  • header_dict: This is required in order to successfully access the API from the script; otherwise the request will return an error. These headers essentially allow the script to mimic a browser making the request. This header dictionary is then passed as a parameter to the request.get() function.

The actual request returns a json-formatted data set (json_set in the example above), which makes it very easy to review and retrieve the sections of data you need. Ultimately, loading the json-formatted data set to a Pandas DataFrame only requires specifying the headers and the actual data element within the json object to the DataFrame constructor.

The above code template can be used to access and load a wide variety of stats categories from the NBA site, and I hope this helps you get started!

--

--