Using Python to Download Yahoo Fantasy Sports Data into a Spreadsheet

I’m the self appointed data steward for the office hockey pool. We award small prizes for the team with the highest points every month. This requires saving the total points and standings by team at the beginning of each month. In years past, that would require me to log in on the first of the month and copy the League Overview table into an excel spreadsheet. As you can imagine, sometimes I’d be off by a few days or even miss a month entirely.

Tracking monthly points for my office hockey pool

As a data enthusiast, I’d like to make visualizations to track each team’s progression throughout the season. So why not get a computer to track points for me? And do it daily instead of monthly!

My first thought was to scrape the data using the html/xml within the webpage. Unfortunately, due to the fact that our league is private, I could not access the page without authentication. After some quick googling, I figured out that the best way to authenticate and pull data from Yahoo Fantasy Sports is to go through their API.

Instead of navigating the API myself, I decided to find a Python (my native programming language) wrapper that helps extract the data from my league. Below you’ll find my guide to extracting data from Yahoo Fantasy Sports and creating an automated workflow to populate the data daily.

Setting up the code

Note: If you’d like access to my compiled code, you can find at my GitHub:

Required software/access:

  • Python 3+
  • Yffpy Python Wrapper
  • Yahoo Account with App that has a Consumer Key and Secret
  • Access to run Task Scheduler

Getting your Fantasy League Information

Consumer Key and Secret: Please follow the detailed steps provided by Yffpy (under the setup section) on how to create an app and get the consumer key (client ID) and secret (client secret) for that app. Fill in your consumer key and secret in your own copy of the private.json.

Game ID: You can either directly query the Yahoo API using Yffpy to get the Game ID or you can use other tools like yfantasysandbox to get your Game ID for the current year. Some common IDs for 2019 are:

  • MLB – 388
  • NFL – 390
  • College Football – 391
  • NBA – 395
  • NHL – 396

League ID: This is easier to get. Log into your league and check the url. The 5 digit code at the end of the url is your league ID.

League ID for your fantasy league

Setting up the Directories and Paths

Excel File: Set up the first sheet of an excel file with the first column as the date and the other columns with the teams in your league in order by team ID ascending. You can get your team IDs information from yfantasysandbox as well. Here’s an example on how my sheet is set up:

Please be sure to update the auth_dir path. This path is the location where youre private.json file is saved. Also update the working_dir path with the path where your points excel file is stored.

Batch file: Update the two paths in the morning-batch.bat file.
The first path should be where your local version of python is stored. If you are using Anaconda and your batch file fails due to an OpenSSL issue, add anaconda to your path. The second path in the batch file should be the location of your file.

Getting Task Scheduler to Retrieve Data Daily

Before you setup the scheduler, try running your batch file and see if it populates a row in your Points.xlsx sheet correctly.

In the Start Menu, type Task Scheduler and click on the application. Data To Fish has provided a great guide on how to run the scheduler daily so it will be able to pull in your fantasy points totals per team every morning.

That’s it! May the fantasy odds be ever in your favour!