Building My NBA Companion With NBA API And Python

Kudzayi Bamhare
5 min readAug 11, 2023

--

Every NBA fan at some point in their lives has found themselves locked in a comically heated basketball debate…

Someone has just walked into the function wearing a Bucks Giannis Antetokounmpo jersey, you feel a slight tightness in your chest but brush it off as nothing.

Tension Building

A few drinks into the night, you overhear this guy proclaim Giannis as the second-greatest Power Forward in the history of the NBA — only behind the great Tim Duncan. Your suspicions about him were true; he’s nothing but a filthy casual, a new-age fanboy who probably only started watching the NBA during the pandemic! You, a true hoops connoisseur, just spent the previous night doing your bi-weekly rewatch of Dirk Nowitzki’s career highlights and spent all of last week trying to perfect his unstoppable one-legged fadeaway for the work tournament on Thursday! The tightness in your chest is now a white burning rage; you must defend Dirk’s honor! But first, you need to quickly look up Dirk’s stats from the 2011 playoffs…

Dirk and Giannis career total pages from the app

There are a few familiar sites for looking up stats for NBA players. Common ones I use are Basketball Reference and StatMuse — both brilliant, beautifully made websites with basketball fans in mind. As a programmer, I couldn’t help but feel like “I should make my own app!” But where do I access the NBA data I need?

The Journey

I finally made the time to begin the process. In my research, I discovered, an API Client for nba.com which gives programmers access to the NBA's vast data so they can create apps that will provide ammunition for "Who is the GOAT" debates for all eternity. The nba_api is a Python API, so I decided to make use of Django as my backend tool. Django's app structure allows me to quickly scale and integrate new features into my project—such as my NBA News feature, which I will briefly touch on.

Dirk's championship playoff-run numbers

Challenges

Getting familiar with a new API always starts as a bit of a challenge. A good thing to do is to get acquainted with the API’s documentation. Not all documentation is made equal, however. nba_api's lacks a bit of expansive detail, but it is still manageable for someone who may be starting. The documentation provides a few examples that can help as a starting point to build on. I used a Conda virtual environment in PyCharm to develop this project. Conda environments can help make it easier to handle large datasets, but the primary benefit they (virtual environments) provide is in managing dependencies and isolating your project's environment.

Giannis championship playoff-run numbers

One of the primary challenges I faced was getting a player’s headshot to display on a player’s profile page. Certain functions, such as players.find_players_by_full_name(), return a lot of important player information but strangely omit a player's headshot image URL. And what's a player page without headshots? The option to brute force it crossed my mind—google a list of all 450+ active NBA players, oh, and retired players as well, search and save their images, and hard code each player image to a player's name! However, because I prefer to live a life of peace, I decided to use one of my favorite Python packages—Beautiful Soup.

Beautiful Soup to the Rescue

Beautiful Soup is a popular Python package used for web scraping and parsing HTML and XML documents. It provides a convenient way to extract data from web pages by navigating the document’s structure, locating specific elements, and retrieving their content.

Headshots and Stats

After a few failed attempts at obtaining headshots from StatMuse, nba.com provided a much more manageable solution. Player profile pages on nba.com use the identical player IDs referenced by the API. This allowed me to create a dynamic solution that can extract any player image as long as I provide the player ID to my function.

My player headshot function

The API doesn’t provide ‘per-game stats’, but I was able to include that information by doing a few calculations.

Django’s dynamic structure allowed me to create the ‘NBA News’ section of the website as its own app within the project. Using Beautiful Soup, I extract the top stories from a sometimes favorite (fellow Lakers fans understand) basketball website of mine, theringer.com/nba.

NBA News feature

Conclusion

In the ever-evolving world of basketball fandom and data, creating my Django NBA app has been an exhilarating journey. From challenging the assertions of questionable Giannis fans to harnessing the power of APIs, web scraping, and Python packages like Beautiful Soup, I’ve managed to build a platform that not only satisfies my personal thirst for NBA stats but also helps fellow enthusiasts dive deep into the numbers that fuel our heated debates. As I continue to enhance the features of my app and refine my coding skills, I’m reminded that the thrill of the game extends beyond the court — into the world of coding, exploration, and discovery!

As the next NBA season draws near, I’ll definitely be returning to this project to further enhance and refine its features. Stay tuned for more slam-dunking code and insights. Until then, may your debates be well-defended!

Link to GitHub repository.

--

--

Kudzayi Bamhare

Habitual code and words writer! Sharing my experiences with tech and other things I enjoy