Skip to main content

Python package to pull sports stats from all major sports leagues.

Project description

SPORTREFPY

Sportrefpy is package that pulls data from the Sports-Reference family of sites. Currently, only the NBA, NHL, and MLB are working but NFL and College Basketball/Football will be supported as soon as possible.

Table of Contents

Installation

pip install sportrefpy

Usage

Each league is more or less set up the same, but they do have some slight differences. Below are some examples for ways that you can use sportrefpy for each sport.

NBA

Initialize a league, team, or player

from sportrefpy.nba.league import NBA
from sportrefpy.nba.team import NBAFranchise
from sportrefpy.nba.player import NBAPlayer

nba = NBA()
sixers = NBAFranchise('PHI')
the_answer = NBAPlayer('Allen Iverson')

Print out Franchise Codes (needed for initializing a team)

from sportrefpy.nba.league import NBA

nba = NBA()
nba.franchise_codes()

Find the career totals (regular + playoffs) of a specific player

from sportrefpy.nba.player import NBAPlayer

# For all stats
king = NBAPlayer('LeBron James')
king.career_totals()

# For a specific stats
beard = NBAPlayer('James Harden')
beard.career_totals(stats=['PTS', 'G'])
  • stats is None by default. If provided, it must be a list even if only using 1.

Compare players stat totals

from sportrefpy.nba.analysis import compare_players

showtime = compare_players(["Shaquille O'Neal", "Kobe Bryant"], 
                            stats=['PTS', 'TRB'],
                            total='career')
  • stats must be a list, with as many stats as you'd like. Required.
  • total defaults to 'career', but can also be 'post' or 'reg'.

Compare Franchise W/L records

from sportrefpy.nba.analysis import compare_franchises

compare_franchises(['NYK', 'BOS'])
  • must be a list of teams, even if only using 1.

Get stats of players for a specific Franchise

from sportrefpy.nba.team import NBAFranchise

bulls = NBAFranchise('CHI')

# All players that have ever played for the team
bulls.players_all_time_stats()

# Or just the GOAT
bulls.players_all_time_stats('Michael Jordan')

Get stats of coaches for a specific Franchise

from sportrefpy.nba.team import NBAFranchise

spurs = NBAFranchise('SAS')

# All coaches that have ever coached the team
spurs.coaches_all_time_data()

# Or just Pop
spurs.coaches_all_time_data('Gregg Popovich')

Get roster for a given season

from sportrefpy.nba.team import NBAFranchise

warriors = NBAFranchise('GSW')
warriors.roster(2016)
  • use integer year that season ends in. This example returns the 2015-16 Golden State Warriors.

Get current season standings by conference

from sportrefpy.nba.league import NBA

nba = NBA()

# Both conferences
east, west = nba.conference_standings()

# Just one of them
west = nba.conference_standings(conf='west')
east = nba.conference_standings(conf='east')

Get franchise season history

from sportrefpy.nba.team import NBAFranchise

mavs = NBAFranchise('DAL')

# Get all seasons
mavs.season_history()

# Get an individual season
mavs.season_history(year='2010-11')
  • year defaults to all seasons. For an individual season, it needs to be a string like in the above example.

Find which seasons a team won the NBA Finals

from sportrefpy.nba.team import NBAFranchise

pistons = NHLFranchise('DET')
seasons = pistons.season_history()
seasons[seasons['Playoffs']  == 'Won Finals']]

NHL

Initialize a league, team, or player

from sportrefpy.nhl.league import NHL
from sportrefpy.nhl.team import NHLFranchise
from sportrefpy.nhl.player import NHLPlayer

nhl = NHL()
flyers = NHL('PHI')
the_great_one = NHL('Wayne Gretzky')

Print out Franchise Codes (needed for initializing a team)

from sportrefpy.nhl.league import NHL

nhl = NHL()
nhl.franchise_codes()

Compare Franchise W/L records

from sportrefpy.nhl.analysis import compare_franchises

compare_franchises(['TBL', 'DET'])
  • must be a list of teams, even if only using 1.

Get roster for a given season

from sportrefpy.nhl.team import NHLFranchise

blues = NHLFranchise('STL')
blues.roster(2019)
  • use integer year that season ends in. This example returns the 2018-19 St. Louis Blues.

Get current season standings by conference

from sportrefpy.nhl.league import NHL

nhl = NHL()

# Both conferences
east, west = nhl.conference_standings()

# Just one of them
west = nhl.conference_standings(conf='west')
east = nhl.conference_standings(conf='east')

Get franchise season history

from sportrefpy.nhl.team import NHLFranchise

flames = NHLFranchise('CGY')

# Get all seasons
flames.season_history()

# Get an individual season
flames.season_history(year='1988-89')
  • year defaults to all seasons. For an individual season, it needs to be a string like in the above example.

Get Stanley Cup winning seasons

from sportrefpy.nhl.team import NHLFranchise

leafs = NHLFranchise('TOR')
seasons = leafs.season_history()
seasons[seasons['Playoffs']  == 'Won Stanley Cup Final']]

NFL

Print out Franchise Codes (needed for initializing a team)

from sportrefpy.nfl.league import NFL

nfl = NFL()
nfl.franchise_codes()

Get conference standings by season

from sportrefpy.nfl.league import NFL

nfl = NFL()

# Both conferences
afc, nfc = nfl.conference_standings()

# Just one of them
nfc = nfl.conference_standings(conf='NFC')
afc = nfl.conference_standings(conf='AFC')

Saving Data

You can write the data to a CSV, JSON, or other file just like you would a Pandas DataFrame

import pandas as pd
from sportrefpy.nba.analysis import compare_franchises

compare_franchises(['IND', 'MIL']).to_csv('comparison.csv')

MLB

Initialize a league, team, or player

from sportrefpy.mlb.league import MLB
from sportrefpy.mlb.team import MLBFranchise
from sportrefpy.mlb.player import MLBPlayer

mlb = MLB()
tigers = MLBFranchise('DET')
hank = MLBPlayer('Henry Aaron')

Print out Franchise Codes (needed for initializing a team)

from sportrefpy.mlb.league import MLB

mlb = MLB()
mlb.franchise_codes()

Compare Franchise W/L records

from sportrefpy.mlb.analysis import compare_franchises

compare_franchises(['MIL', 'LAA'])
  • must be a list of teams, even if only using 1.

Get stats of batters for a specific Franchise

from sportrefpy.mlb.team import MLBFranchise

dodgers = MLBFranchise('LAD')

# All players that have ever played for the team
dodgers.batters_all_time_stats()

# Or just one player
dodgers.batters_all_time_stats('Jackie Robinson')

Get stats of pitchers for a specific Franchise

from sportrefpy.mlb.team import MLBFranchise

pirates = NBAFranchise('PIT')

# All players that have ever played for the team
pirates.players_all_time_stats()

# Or just one pitcher with a cool name
pirates.pitchers_all_time_stats('High Pockets Kelly')

Get stats of managers for a specific Franchise

from sportrefpy.mlb.team import MLBFranchise

reds = MLBFranchise('CIN')

# All managers for that team
reds.coaches_all_time_data()

# Or just Buck
reds.managers_all_time_stats('Buck Ewing')

Get regular season batting stats

from sportrefpy.mlb.player import MLBPlayer

the_kid = MLBPlayer('Ken Griffey Jr.')

# Get all seasons stats
the_kid.regular_season_batting()

# Or a specific season
the_kid.regular_season_batting(2009)
  • post_season_batting() functions the same way

Get regular season pitching stats

from sportrefpy.mlb.player import MLBPlayer

sandy = MLBPlayer('Sandy Koufax')

# Get all seasons stats
sandy.regular_season_pitching()

# Or a specific season
sandy.regular_season_pitching(1966)
  • post_season_pitching() functions the same way

Get regular season fielding stats

from sportrefpy.mlb.player import MLBPlayer

cal = MLBPlayer('Cal Ripken Jr.')

# Get all seasons stats
cal.regular_season_fielding()

# Or a specific season
cal.regular_season_fielding(1996)

Get career pitching stats

from sportrefpy.mlb.player import MLBPlayer

madbum = MLBPlayer('Madison Bumgarner')

# Get all pitching totals
madbum.career_totals_pitching()

# Or a specific stat
madbum.career_totals_pitching('IP')
  • stat defaults to the current season if nothing is provided.

Get roster for a given season

from sportrefpy.mlb.team import MLBFranchise

braves = MLBFranchise('ATL')
braves.roster(1995)

Get league and conference standings by year

from sportrefpy.mlb.league import MLB

mlb = MLB()

# Overall standings
standings = mlb.standings(season=2018)

# Just one of them
al = mlb.al_standings(season=1990)
nl = mlb.conference_standings()
  • season defaults to the current season if nothing is provided.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sportrefpy-0.3.0.tar.gz (209.8 kB view details)

Uploaded Source

Built Distribution

sportrefpy-0.3.0-py3-none-any.whl (217.8 kB view details)

Uploaded Python 3

File details

Details for the file sportrefpy-0.3.0.tar.gz.

File metadata

  • Download URL: sportrefpy-0.3.0.tar.gz
  • Upload date:
  • Size: 209.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.6

File hashes

Hashes for sportrefpy-0.3.0.tar.gz
Algorithm Hash digest
SHA256 73f843762292a66df585fb6ec8e6585ba700acb91499e003cccf8bd1b9aa657a
MD5 9b5b377347b976cd5864f888c78d55fb
BLAKE2b-256 4199b06ca8ae6e81e82f32c7471fc9fb8e0197737b273a11728ae6506e7f984c

See more details on using hashes here.

File details

Details for the file sportrefpy-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: sportrefpy-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 217.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.6

File hashes

Hashes for sportrefpy-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 704a105dffc7f25fcd81f2686f4ae29abd877bde22dd09329c2e63b1a54086a8
MD5 c350bf65498f51902dfe28e589480162
BLAKE2b-256 35b57f1971b978522686fe2aaadd1ec342baa0faad57c055d3c28c811441a204

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page