Historical soccer results in Python: a club's seasons and head-to-heads since 2010
A command-line script with two modes: seasons prints a club's league position, record, goals and points for every season on file; h2h lists every league meeting between two clubs with the win/draw/loss tally. Both can write CSV. Useful in the off-season, for previews, or as training data.
- Stack
- Python, requests
- Plan
- Free (a full Premier League history is 18 calls)
- Code
- python-soccer-history/ (MIT)
Updated 2026-11-23. Tested against the live API on that date. Data is aggregated from public sources and is typically 20-30 seconds behind live play (about 1 second after our source). There is no SLA.
1. Get a free API key
Sign up (no card) and copy the key from the dashboard. The Free plan gives you 1,000 calls in the first 30 days, then 125 a month, at 1 request per second. Every successful REST call counts as one call.
Check the key works with one call to the live-scores endpoint:
Terminal
export REALTIME_SPORTS_API_KEY=your_key
curl -H "Authorization: Bearer $REALTIME_SPORTS_API_KEY" \
"https://www.realtimesportsapi.com/api/v1/sports/football/leagues/nfl/events/live"2. Get the code
Everything below is in the python-soccer-history/ folder of the examples repo (MIT). Clone it and work from that folder.
Terminal
git clone https://github.com/ElcoDevRepos/realtime-sports-api-examples
cd realtime-sports-api-examples/python-soccer-history3. Install
Python 3.10+ with requests.
Terminal
python3 -m venv .venv && . .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env # REALTIME_SPORTS_API_KEY4. How it works
/history lists the seasons on file for a league, with how many matches each has and how many have a result. seasons then reads each season's table from /history/{season}/standings and picks out the club; a season where the club played in another division is skipped. h2h reads each season's matches for the first club with ?team= (a slug or the start of a name) and keeps the games against the second.
Tables are computed from the results: 3 points a win, no deductions. They can differ from an official table where a club had points deducted. A season with results still missing on file is marked "not complete". Every history response says data_type: "historical" and live: false: this is past data, updated periodically, not live scores.
python-soccer-history/history.py
#!/usr/bin/env python3
"""Historical soccer results with the Realtime Sports API: a club's season-by-season record and
head-to-head results between two clubs, back to 2010-11 for most leagues.
Uses the historical results endpoints (on every plan, including Free). Historical data is updated
periodically and is never live: for live scores use the events endpoints.
Usage:
python history.py seasons arsenal # Premier League, every season on file
python history.py seasons barcelona --league esp.1
python history.py h2h arsenal tottenham # every Premier League meeting
python history.py h2h "manchester united" liverpool --since 2015-16 --csv h2h.csv
Team names: a slug (arsenal-fc) or the start of a name ("manchester united"). Costs one API call
for the season list, plus one per season read.
"""
from __future__ import annotations
import argparse
import csv
import os
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional
import requests
API_BASE = os.environ.get("RSA_API_BASE", "https://www.realtimesportsapi.com/api/v1")
def load_dotenv(*paths: Path) -> None:
"""Tiny .env loader (KEY=value). Existing environment variables win."""
for path in paths:
if not path.exists():
continue
for line in path.read_text().splitlines():
line = line.strip()
if line and not line.startswith("#") and "=" in line:
k, v = line.split("=", 1)
os.environ.setdefault(k.strip(), v.strip().strip("'\""))
def api_get(path: str, key: str, **params: Any) -> Optional[Dict[str, Any]]:
"""GET a history endpoint. Returns None on 404 (no data for that season/table)."""
res = requests.get(
API_BASE + path,
params={k: v for k, v in params.items() if v is not None},
headers={"Authorization": f"Bearer {key}"},
timeout=30,
)
if res.status_code == 404:
return None
if not res.ok:
try:
err = res.json().get("error") or {}
except ValueError:
err = {}
sys.exit(f"HTTP {res.status_code} {err.get('code', '')}: {err.get('message', res.text[:200])}")
return res.json()
def seasons_on_file(league: str, key: str, since: Optional[str]) -> List[Dict[str, Any]]:
data = api_get(f"/sports/soccer/leagues/{league}/history", key)
if not data:
sys.exit(f"No historical results for league {league}.")
rows = sorted(data["data"]["seasons"], key=lambda s: s["season"])
return [s for s in rows if not since or s["season"] >= since]
def norm(s: str) -> str:
return s.lower().replace("-", " ").strip()
def season_by_season(team: str, league: str, key: str, since: Optional[str]) -> List[Dict[str, Any]]:
out = []
for s in seasons_on_file(league, key, since):
table = api_get(f"/sports/soccer/leagues/{league}/history/{s['season']}/standings", key)
if not table:
print(f" {s['season']}: no league table (cup or group competition)", file=sys.stderr)
continue
rows = table["data"]["standings"]
hit = next((r for r in rows if r["team"]["slug"] == team or norm(r["team"]["name"]).startswith(norm(team))), None)
if not hit:
continue # not in this division that season (promoted/relegated)
out.append({
"season": s["season"],
"complete": s["played"] == s["matches"],
"position": hit["position"], "of": len(rows),
"team": hit["team"]["name"],
"played": hit["played"], "won": hit["won"], "drawn": hit["drawn"], "lost": hit["lost"],
"goals_for": hit["goals_for"], "goals_against": hit["goals_against"], "points": hit["points"],
})
return out
def head_to_head(a: str, b: str, league: str, key: str, since: Optional[str]) -> List[Dict[str, Any]]:
out = []
for s in seasons_on_file(league, key, since):
data = api_get(f"/sports/soccer/leagues/{league}/history/{s['season']}/matches", key, team=a)
if not data:
continue
a_slugs = {t["slug"] for t in data["data"]["teams"]}
for m in data["data"]["matches"]:
home, away = m["home"], m["away"]
other = away if home["slug"] in a_slugs else home
if not (other["slug"] == b or norm(other["name"]).startswith(norm(b))):
continue
sc = m.get("score") or {}
if m.get("status") != "final" or sc.get("home") is None:
continue
a_home = home["slug"] in a_slugs
gf, ga = (sc["home"], sc["away"]) if a_home else (sc["away"], sc["home"])
out.append({
"season": s["season"], "date": m["date"], "round": m.get("round", ""),
"home": home["name"], "away": away["name"],
"score": f"{sc['home']}-{sc['away']}",
"result": "W" if gf > ga else "L" if gf < ga else "D",
})
return out
def write_csv(rows: List[Dict[str, Any]], path: str) -> None:
if not rows:
return
with open(path, "w", newline="") as f:
w = csv.DictWriter(f, fieldnames=list(rows[0].keys()))
w.writeheader()
w.writerows(rows)
print(f"Wrote {len(rows)} rows to {path}")
def main() -> None:
load_dotenv(Path(__file__).with_name(".env"), Path.cwd() / ".env")
key = os.environ.get("REALTIME_SPORTS_API_KEY")
if not key:
sys.exit("Set REALTIME_SPORTS_API_KEY (or put it in .env). Free key: https://www.realtimesportsapi.com/signup")
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
sub = ap.add_subparsers(dest="cmd", required=True)
p1 = sub.add_parser("seasons", help="a club's league position and record, season by season")
p1.add_argument("team")
p2 = sub.add_parser("h2h", help="every league meeting between two clubs")
p2.add_argument("team_a")
p2.add_argument("team_b")
for p in (p1, p2):
p.add_argument("--league", default="eng.1", help="eng.1, eng.2, esp.1, ita.1, ger.1, fra.1, mex.1, uefa.champions")
p.add_argument("--since", help="first season, e.g. 2015-16")
p.add_argument("--csv", help="also write the rows to this CSV file")
args = ap.parse_args()
if args.cmd == "seasons":
rows = season_by_season(args.team, args.league, key, args.since)
if not rows:
sys.exit(f"No seasons found for {args.team!r} in {args.league}.")
print(f"{rows[0]['team']} in {args.league}, season by season")
print(f"{'Season':<9} {'Pos':>7} {'P':>3} {'W':>3} {'D':>3} {'L':>3} {'GF':>4} {'GA':>4} {'Pts':>4}")
for r in rows:
flag = "" if r["complete"] else " (season not complete on file)"
print(f"{r['season']:<9} {str(r['position']) + '/' + str(r['of']):>7} {r['played']:>3} {r['won']:>3} {r['drawn']:>3} {r['lost']:>3} "
f"{r['goals_for']:>4} {r['goals_against']:>4} {r['points']:>4}{flag}")
else:
rows = head_to_head(args.team_a, args.team_b, args.league, key, args.since)
if not rows:
sys.exit(f"No finished meetings found between {args.team_a!r} and {args.team_b!r} in {args.league}.")
for r in rows:
print(f"{r['date']} {r['home']} {r['score']} {r['away']} ({r['result']} for {args.team_a})")
w = sum(r["result"] == "W" for r in rows); d = sum(r["result"] == "D" for r in rows); l = sum(r["result"] == "L" for r in rows)
print(f"\n{len(rows)} meetings: {args.team_a} won {w}, drew {d}, lost {l}")
if args.csv:
write_csv(rows, args.csv)
if __name__ == "__main__":
main()
5. Run it
Run against the live API on 23 November 2026: seasons arsenal --since 2019-20 printed Arsenal's record from 8th in 2019-20 to 2nd in 2022-23, 2023-24 and 2024-25 (89 points in 2023-24), with the two seasons still in progress flagged. h2h arsenal tottenham --since 2021-22 found 10 league meetings: Arsenal won 8, drew 1 and lost 1.
Terminal
python history.py seasons arsenal
python history.py seasons barcelona --league esp.1 --since 2015-16
python history.py h2h arsenal tottenham --csv h2h.csv
python history.py h2h "manchester united" liverpool --since 2015-16Endpoints used
GET /sports/soccer/leagues/{league}/historyGET /sports/soccer/leagues/{league}/history/{season}/standingsGET /sports/soccer/leagues/{league}/history/{season}/matches?team=
Full reference: docs · OpenAPI · what each endpoint returns, by league
FAQ
- Which leagues and seasons are covered?
- Premier League, Championship and Bundesliga from 2010-11, Champions League from 2011-12, LaLiga from 2012-13, Serie A from 2013-14, Ligue 1 from 2014-15 and Liga MX from 2018-19, through the current season.
- Is the historical data on the Free plan?
- Yes. The history endpoints are on every plan, and each request counts as one call like any other.
- Is this live data?
- No. It is historical, updated periodically, and every response is marked live: false. For live scores and today's fixtures use the events endpoints.
- Why does a table differ from the official one?
- Tables are computed from match results with 3 points for a win and no deductions, so a season in which a club was docked points will not match the official table.