Skip to content

Instantly share code, notes, and snippets.

@sertalpbilal
Last active June 30, 2021 21:00
Show Gist options
  • Save sertalpbilal/796ee38f9fe38f1b556cbdb4bda4d765 to your computer and use it in GitHub Desktop.
Save sertalpbilal/796ee38f9fe38f1b556cbdb4bda4d765 to your computer and use it in GitHub Desktop.
Download FPL API data
import requests
import json
import os
from pathlib import Path
BASE = "https://fantasy.premierleague.com/api/"
season = "2020-21"
API = f"data/{season}/"
TEAM = 2221044 # REPLACE YOUR TEAM ID HERE
Path(API).mkdir(parents=True, exist_ok=True)
def cache_page_as(page, address):
URL = BASE + page
TARGET = API + address
if os.path.exists(TARGET):
print("Existing target... skipping")
return
print(f"Requesting {URL} -> {TARGET}")
r = requests.get(URL)
if r.status_code != 200:
print(r.status_code)
print(f"Error in request {page}")
return
os.makedirs(os.path.dirname(TARGET), exist_ok=True)
with open(TARGET, 'w') as f:
json.dump(r.json(), f, indent=2)
cache_page_as("bootstrap-static/", "main.json")
cache_page_as("fixtures/", "fixtures/all.json")
for gw in range(1,39):
cache_page_as(f"fixtures/?event={gw}", f"fixtures/{gw}.json")
for gw in range(1,39):
cache_page_as(f"event/{gw}/live", f"live/{gw}.json")
cache_page_as(f"entry/{TEAM}/", "team_main.json")
cache_page_as(f"entry/{TEAM}/history/", "team_history.json")
cache_page_as(f"entry/{TEAM}/transfers/", "team_transfers.json")
for gw in range(1,39):
cache_page_as(f"entry/{TEAM}/event/{gw}/picks/", f"picks/{gw}.json")

You can save the python file to your machine, replace your team ID and run

python download_fpl_api.py

This will create a directory data and will include all files in JSON format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment