How to get NBA statistics of your favorite players with a Python function | by Edoardo Romani | Towards Data Science

This article will show you one of the ways you can deploy a Python function to simulate a task as simple as searching for your favorite basketball player’s statistics for a given NBA season.
As a big NBA basketball fan, I often found myself looking up online to stay informed over a particular player’s performance, and wondered whether I could have fun trying to replicate the process using Python, with the idea of simply launching searches and getting the results from the command line of my laptop, without actually opening my browser and getting online.
Having Python do web-searches for you can be incredibly powerful for many different types of applications, and I hope this brief example can bring its usefulness to life and inspire you to apply these concepts for your own personal projects.
In the case of the basketball analytics community, BasketballReference.com is a goldmine of data, since it features all sorts of relevant in-game statistics useful for many basketball analytics projects. I will be using this website to collect the relevant data.
Particularly for NBA statistics, the site has a “Per Game” section where one can obtain Per Game performance statistics for any relevant category across the entire population of ~500 NBA players taking part in any given season.
The goal is to compile and execute a Python program which can return any player’s complete Per Game statistics, for any given season. The site has recorded Per Game stats since the beginining of the 1950s, so that gives you around 70 (!) years’worth of data that you can search.
Let’s now go through the function’s code required to make this work and break it down step by step.
Import relevant Packages: As a first step , you will need to import the relevant Python packages and define the url variable, which points to the online data source. You should also import the pandas package (“import pandas as pd”) as you will need it later on.
Import relevant Packages: As a first step , you will need to import the relevant Python packages and define the url variable, which points to the online data source. You should also import the pandas package (“import pandas as pd”) as you will need it later on.
import requestsfrom bs4 import BeautifulSoupimport pandas as pd
2. Define the function and get input from the user
You define the function with the def keyword and give it a representative name, such as get_NBA_stats; the function will not take any parameters but instead will have the user define them via two input commands, in which the program prompts for the year and the players’name for which to return per-game statistics.
def get_NBA_stats():year=input("Which NBA season are you interested in?: ")player=input("For which player do you want to get stats?: ")
3. Scrape and save data
You then need to fetch the relevant url content with the request.get method. Note how the year variable is used to point the search to the webpage where the year seasons’ stats reside. Then, you can extract its text with the .text method, and finally parse it into a Beatiful Soup object.
url = 'https://www.basketball-reference.com/leagues/NBA_{}_per_game.html'.format(year)   r = requests.get(url)r_html = r.textsoup = BeautifulSoup(r_html,'html.parser')
In order to locate the table with the players’ dataset within the HTML of the BasketballReference webpage, you can inspect the code of the page (this can be done by clicking Ctrl + Shift + I on Google Chrome) and see that the table element you are interested in has a class equal to full_table which you can use to store table contents into the table variable, through the soup.find_all command.
You can then use soup.find to locate the element in a page with class equal to table_head, since this class stores the column names of our players’dataset. You can save them in a variable named head, which is then used to extract and clean the relevant column names for your final dataset, such as each player’s name, point per game, rebound per game, etc.. as you can see below.
table=soup.find_all(class_="full_table")    """ Extracting List of column names"""head=soup.find(class_="thead")column_names_raw=[head.text for item in head][0]    column_names_polished=column_names_raw.replace("\n",",").split(",")[2:-1]print(column_names_polished)['Player', 'Pos', 'Age', 'Tm', 'G', 'GS', 'MP', 'FG', 'FGA', 'FG%', '3P', '3PA', '3P%', '2P', '2PA', '2P%', 'eFG%', 'FT', 'FTA', 'FT%', 'ORB', 'DRB', 'TRB', 'AST', 'STL', 'BLK', 'TOV', 'PF', 'PTS']
You can then iterate over the entire table element, and for every row of data (related to a single player and identified by the td HTML tag) you can extract all player’s statistics, and store them into a list, named players. All you have to do once this is done is store all players’ information into a pandas DataFrame, which takes the column names previously defined and to which you can set the Player’s full name as the index.
Note: obtaining the entire list of players for a given season is not necessary for achieving the specific task at hand, I am just showing how to do so as it usually is best practice to store all potentially useful data (e.g if later on you would want to compare a single player’s performance against its peers, having the full sample of NBA Players would be beneficial).
"""Extracting full list of player_data"""players=[]        for i in range(len(table)):                player_=[]                for td in table[i].find_all("td"):            player_.append(td.text)            players.append(player_)df=pd.DataFrame(players, columns=column_names_polished).set_index("Player")#cleaning the player's name from occasional special charactersdf.index=df.index.str.replace('*', '')
4. Close the function by returning your requested player stats
You can then end the function by displaying the full stats for our requested player, whose name you,the user, specified at the begining of the function’s body, and print it to the screen.
print(df.loc[player])if __name__ == "__main__":    get_NBA_stats()
Your function is now set! All you have to do now is save the python file (I called mine NBA_per_game.py) and execute the file whenever you want, for any player and year combination you want, right from the command line of your PC.
Remember to save the file in a directory which you can easily access from the command line in order to launch the program in a heartbeat.
I saved mine on my Desktop so that it was easily accessible and then recorded a couple of executions in the below video.
Your function is officially set up and runs in no time. It is quite a neat solution you can adopt if you find yourself searching for this kind of information over and over again, and shows how web-scraping can be very helpful in speeding up even the simplest of tasks.
I am a big fan how web-scraping packages and have also used them for other purposes, such as pulling financial stock data using APIs.
Hope you’ll find this useful the next time you’ll be want to have a brief look at some NBA stats (while perhaps preparing your Fantasy Basketball lineup!).
Access my free Data Science resource checklist here