API Setup
For those unfamiliar with APIs or data analysis in general, you can think of them as a translator. In general, they just allow two applications to communicate with one another. One of the most popular uses for APIs are “web scrapes,” which allow a user to pull (or “scrape”) a bunch of information from a website and utilize that information in another application. For my use case, I am relying upon this amazing API to translate information from ESPN’s website (written in HTML) to a clean format in Python. The setup for this API is impressively simple; just import the package and find your league IDs and you are up and running. The setup is shown below:
Initial setup of espn_api, with necessary inputs for a private league
Two inputs are needed to connect to public leagues (that anyone can see) and four are needed for private leagues. Your “year” can be any season that you want to look at, and goes back as far as your league has been active on ESPN; I will be looking at 2022. Your “ league_id” is in every URL when you are logged in and on your ESPN Fantasy Football website. All you need to do is expand the URL to find your league_id, as shown below:
Expand your URL to show your “league_ID” and currently selected “year” (at the end)
Since mine is a private league, I had to go through the steps of finding my “swid” and “espn_s2” IDs. These are surprisingly simple to get, and all you will need to do on a Mac is click OPTION-COMMAND-I to open the “web inspector” tool, and then navigate to “Storage” and expand the “Cookies” section on the left (as shown below). If on Windows, you can generally right-click and select “inspect element” to yield the same type of web inspector. The espn_s2 and swid inputs that you need will each be stored as a cookie if you are in a private league. Copy/paste these into the “league” inputs and then run the print statement at the bottom, which should now show all the teams in your league!
The “Web Inspector,” used by Mac, cleanly shows the cookies that you will need to connect to your private league.