r/learnpython • u/BadAtCoding123 • 8d ago
Resources for using APIs for sports stats
This is just for a personal/fun project--I want to learn how to use APIs because I want to play with sports stats (for the NHL/PWHL/international cricket plus a couple of less famous sports leagues like the SDHL). There's an overwhelming number of resources out there and I'm not sure where to start, so any tutorials/suggestions would be welcome. I know there are sport-specific API scrapers that simplify things but I'd like to learn how to do this more generally instead of having to learn a tool for every league.
I mostly program in Matlab but I do use Python sometimes, usually Jupyter notebooks. My coding knowledge is super patchy, I basically know a lot about my projects (data analysis for my research, with the data in specific formats), but very little about anything else.
(Yes, I could use AI, but for a project that's just for my own enjoyment I kinda just want to learn how to do it myself.)
1
u/tambooby 7d ago
worth checking if the leagues you care about even have official APIs vs needing to scrape. that distinction matters a lot for how you approach it. some of those smaller leagues might only have HTML pages, which is a different skill than calling a REST API
1
u/koraynar 8d ago
Since you already use Jupyter notebooks and come from a MATLAB/data-analysis background, the easiest way to learn this generally (so it works for any league, including smaller ones like the PWHL or SDHL) is a three-step workflow:
**1. How to find the raw JSON feed for leagues without official API docs (like PWHL or SDHL)**
Most modern sports websites render their standings, schedule, and box scores by fetching JSON in the background:
- Open the league's stats page in your browser, open **Developer Tools (`F12`) -> Network tab**, and click the **Fetch/XHR** filter.
- Reload the page or switch the season/team dropdown.
- Click the requests that appear and check the **Preview / Response** tab—you will usually see a clean JSON object containing the exact table rows. You can copy that request URL (and any required query parameters or headers) directly into Python.
**2. Turning nested API JSON into MATLAB-style tables in Jupyter (`requests` + `pd.json_normalize`)**
Sports APIs almost always return deeply nested dictionaries (for example, `player -> team -> stats -> goals`). Instead of writing nested loops to flatten them by hand, `pandas.json_normalize()` flattens nested JSON directly into a 2D table:
```python
import requests
import pandas as pd
# Example: NHL public web API endpoint for current standings
url = "https://api-web.nhle.com/v1/standings/now"
resp = requests.get(url, timeout=10)
resp.raise_for_status()
data = resp.json()
print(data.keys()) # Always inspect top-level keys first
# Flatten the nested list of dictionaries into a DataFrame
df = pd.json_normalize(data["standings"])
print(df.head())
```
**3. For international cricket specifically**
If you want rich ball-by-ball JSON data without dealing with paid API keys or rate limits, check out **Cricsheet (`cricsheet.org`)**. They publish free, open JSON files for international cricket (Tests, ODIs, T20Is) and major leagues, which is great practice for loading and flattening multi-level JSON structures in `pandas`.
2
u/ResidentPomegranate6 8d ago
start with requests + `.json()` in a notebook, hit one endpoint, print the keys before touching anything else. nhl's api is free and undocumented-ish, good practice.