It’s one of my favorite times of year. The weather is cooling, kids are going back to school (phew), and the thrill of Fantasy Football seasons are upon us. Since 2010, a group of my college friends have played in a competitive Fantasy Football league that awards a trophy and a lot of pride to the winner each year.
We have typically tracked season scores in a Google spreadsheet that required a lot of weekly manual inputs and updates each year. But no more! We have too many kids and responsibilities to be doing manual data work. Inspired by some existing resources online, I wrote two Python scripts, which you can download at the bottom of this piece for your own use, and a Tableau dashboard to scrape Fantasy Football data from ESPN and visualize it in an automated way.
Attached, you’ll find two Python scripts that are very similar, but call on different ESPN APIs. One script scrapes historical league data and the other scrapes data for the current season. The result of each script is written to a Google spreadsheet that connects to this Tableau dashboard, which will also be embedded below in case you just want to see the end result.
How to Adopt This for Your ESPN League
To use the Python scripts provided, you will need a few pieces of information from ESPN: values for the league ID, espn_s2, and the swid. The league ID can be found in the URL of your ESPN Fantasy Football homepage:
The espn_s2 and swid can be found by navigating to the ESPN Fantasy Football homepage, right clicking, and selecting “Inspect.” Click “Application” from the top menu and select https://fantasy.espn.com from the left menu. Values for espn_s2 and swid will appear a little down the list:
The Python files are documented so that each section of code is explained. Some of the code is league specific, so depending on your league’s schedule and rules, you may need to adjust the logic. Some of the code might be optional for your league, and you may need to remove it. A lot of the initial code in the script uses guidance from this YouTube video and GitHub repository in case it is helpful to reference these resources as well.
The result of the Python code is a Google sheet with data populated in the following format. Data is structured so that each row represents a unique year, week, and team. Where possible, I calculated aggregations before the data reached Tableau. This eliminated the need for extra calculations and simplified visuals in Tableau:
I connected the Google Sheet to Tableau using a live connection and created this Alumni Fantasy Football League Dashboard. The visuals and navigation are pretty simple. The first four tabs reflect data from a selected season. Usually this would be the most current season, but users can select the football icon in the top right corner of the page to select any historical season as well. The last two tabs were inspired by feedback from members of the league. They show how teams have performed historically compared to the league and compared to specific teams. The goal of these two tabs was to “increase trash talk” and “strengthen rivalries” between teams.
Feel free to play around with the dashboard here:
Some of the terms that you need to be familiar with when navigating the Python code and Tableau dashboard are below:
- Points For is the number of points the team scored for the given week.
- Points Against is the number of points a team’s opponent scored against them for the given week.
- Top 50% refers to whether the team was in the top half of “Points For” in the given week.
- Weekly Score for this league awards two points for a team win against an opponent and one point for a team that scored in the Top 50% of teams for the week. A team’s weekly score may be anywhere from zero to three points.
- Cumulative Score adds the Weekly Scores up over the course of a season. If a team scores one point in the first week of the season and 2 points in the second week of the season, their Cumulative Score would be 3 points.
- Ranking refers to the team’s rank compared to the other teams in the league. For the more recent seasons, this ranking depends first on a team’s Cumulative Score, then Cumulative Wins for the season and, finally, Cumulative Points For over the season.
I had a lot of fun with this project. Putting together the Python code stretched my coding skills and the Tableau dashboard allowed me to try out different color schemes, design and charts than I normally would in client work. Plus, I got to appreciate how good I am at Fantasy Football 😊
Feel free to download the Python scripts I used by clicking here.