Package: pysports-stats
Development Status: This project is no longer undergoing active development. Please consider
opening a pull request for any new features or bug fixes to be reviewed and
merged.ContentsInstallationExamplesGet instances of all NHL teams for the 2018 seasonPrint every NBA team’s name and abbreviationGet a specific NFL team’s season informationPrint the date of every game for a NCAA Men’s Basketball teamPrint the number of interceptions by the away team in a NCAA Football gameGet a Pandas DataFrame of all stats for a MLB gameFind the number of goals a football team has scoredDocumentationTestingsports is a free python API that pulls the stats from
www.sports-reference.com and allows them to be easily be used in python-based
applications, especially ones involving data analytics and machine learning.sports exposes a plethora of sports information from major sports
leagues in North America, such as the MLB, NBA, College Football and Basketball,
NFL, and NHL. sports also now supports Professional Football (or
Soccer) for thousands of teams from leagues around the world. Every sport has
its own set of valid API queries ranging from the list of teams in a league, to
the date and time of a game, to the total number of wins a team has secured
during the season, and many, many more metrics that paint a more detailed
picture of how a team has performed during a game or throughout a season.NOTE (2022-11-23): The source website has implemented traffic restrictions that
will result in your IP being temporarily blacklisted if you exceed 20 requests
per minute. Therefore all outgoing requests are now passed through a utility function
that ensures you will not surpass the rate limit threshold. This runtime slowdown
is most noticeable for sports with many teams. If you are trying to bulk gather data
you will need to schedule a long running job, and its recommended that you cache any
historical data you intend to reuse.InstallationTODO: This has not been released to Pypi yet, so standard pip install will not work.
But I’m keepin the documentation as a template for now.The easiest way to installpysports-statsis by downloading the latest
released binary from PyPI using PIP. For instructions on installing PIP, visitPyPA.iofor detailed steps on
installing the package manager for your local environment.Next, run:pip install pysports-statsto download and install the latest official release ofpysports-statson
your machine. You now have the latest stable version ofpysports-statsinstalled and can begin using it following the examples below!If the bleeding-edge version ofpysports-statsis desired, clone this
repository using git and install all of the package requirements with PIP:git clone https://github.com/bveber/pysports-stats
cd pysports-stats
poetry installOnce complete, create a Python wheel for your default version of Python by
running the following command:poetry buildThis will create a.whlfile in thedistdirectory which can be installed
with the following command:pip install dist/*.whlExamplesThe following are a few examples showcasing how easy it can be to collect
an abundance of metrics and information from all of the tracked leagues. The
examples below are only a miniscule subset of the total number of statistics
that can be pulled using pysports-stats. TODO: Create documentationGet instances of all NHL teams for the 2018 seasonfromsports.nhl.teamsimportTeamsteams=Teams(2018)Print every NBA team’s name and abbreviationfromsports.nba.teamsimportTeamsteams=Teams()forteaminteams:print(team.name,team.abbreviation)Get a specific NFL team’s season informationfromsports.nfl.teamsimportTeamsteams=Teams()lions=teams('DET')Print the date of every game for a NCAA Men’s Basketball teamfromsports.ncaab.scheduleimportSchedulepurdue_schedule=Schedule('purdue')forgameinpurdue_schedule:print(game.date)Print the number of interceptions by the away team in a NCAA Football gamefromsports.ncaaf.boxscoreimportBoxscorechampionship_game=Boxscore('2018-01-08-georgia')print(championship_game.away_interceptions)Get a Pandas DataFrame of all stats for a MLB gamefromsports.mlb.boxscoreimportBoxscoregame=Boxscore('BOS201806070')df=game.dataframeFind the number of goals a football team has scoredfromsports.fb.teamimportTeamtottenham=Team('Tottenham Hotspur')print(tottenham.goals_scored)DocumentationThis repository is based onsportsipy. I am incredibly grateful for
the work done by Robert Clark in creating this code base. Some internal components
have been changed, but the basic interface remains the same.
Two blog posts detailing the creation and basic usage ofsportsipycan
be found on The Medium at the following links:Part 1: Creating a public sports APIPart 2: Pull any sports metric in 10 lines of PythonThe second post in particular is a great guide for getting started withpysports-statsand is highly recommended for anyone who is new to the
package.TestingPysports-stats contains a testing suite which aims to test all major portions
of code for proper functionality. To run the test suite against your
environment, ensure all of the requirements are installed by running:pip install -r requirements.txt
pip install coverage flexmock mock pycodestyle pytest pytest-cov pytest-xdistNext, start the tests by running py.test while optionally including coverage
flags which identify the amount of production code covered by the testing
framework:py.test --cov=sports --cov-report term-missing tests/If the tests were successful, it will return a green line will show a message at
the end of the output similar to the following:======================= 752 passed in 158.03s (0:02:38) =======================If a test failed, it will show the number of failed and what went wrong within
the test output. If that’s the case, ensure you have the latest version of code
and are in a supported environment. Otherwise, create an issue on GitHub to
attempt to get the issue resolved.NOTE (2022-11-23): Due to traffic restriction on the source webpage the exhaustive
are unusable. They have been commented out for now, but should be replaced with a more
sensible set of tests to validate connectivity.