In this article, we will see how sports.py module is imported and implemented to produce scoreboard of a specified sport like baseball, basketball, cricket and many more along with other details about the sport. Code examples in the text below revolve around cricket, you can perform the same operation for any other sport.
Sport.py Scrapes data from:
- scorespro.com
- pro-football-reference.com
- baseball-reference.com
- basketball-reference.com
- hockey-reference.com
Not all sports are supported by this module, all of which are supported are listed below along with their respective python code to reference them:
SPORT |
PYTHON REFERENCE |
Baseball | sports.BASEBALL |
Basketball | sports.BASKETBALL |
Cricket | sports.CRICKET |
Handball | sports.HANDBALL |
Football | sports.FOOTBALL |
Hockey | sports.HOCKEY |
Rugby Union | sports.RUGBY_U |
Rugby League | sports.RUGBY_L |
Soccer | sports.SOCCER |
Tennis | sports.TENNIS |
Volleyball | sports.VOLLEYBALL |
Installation
First, we need to install this module and for that just simply run the following code into your terminal:
pip install sports.py
Implementation
- import module
- Get a single match using get_match()
Syntax-
get_match(sport, team1, team2)
get_match() returns a single Match object which contains the following properties:
PROPERTY |
DESCRIPTION |
sport | Name of the sport |
league | League of the match |
home_team | Home team |
away_team | Away team |
home_score | Home team score |
away_score | Away team score |
match_time | Current match time |
match_date | Date on which the match was played |
match_link | Link to an XML file containing match data |
Example 1:
Python3
# import module import sports # setting sport sports.get_match(sports.CRICKET, 'KINGS XI PUNJAB' , 'ROYAL CHALLENGERS BANGALORE' ) |
Output:
Example 2:
Program that prints all live cricket matches.
Python3
# import module import sports # extracting information matches = sports.get_sport(sports.CRICKET) #printing all matches for item in matches: print (item) |
Output:
Example 3:
An application that produces cricket live scores using tkinter in a GUI format.
Python3
# import modules from tkinter import * import sports from tkinter import messagebox def cricket_info(): try : match = sports.get_match(sports.CRICKET, e1.get() , e2.get()) date. set (match.match_date) time. set (match.match_time) league. set (match.league) team1. set (match.home_team) team2. set (match.away_team) team1_score. set (match.away_score) team2_score. set (match.home_score) link. set (match.match_link) except : messagebox.showerror( "showerror" , "No match found" ) # object of tkinter # and background set for light grey master = Tk() master.configure(bg = 'light grey' ) # Variable Classes in tkinter date = StringVar(); time = StringVar(); league = StringVar(); team1 = StringVar(); team2 = StringVar(); team1_score = StringVar(); team2_score = StringVar(); link = StringVar(); # Creating label for each information # name using widget Label Label(master, text = "Team 1 :" , bg = "light grey" ).grid(row = 0 , sticky = W) Label(master, text = "Team 2 :" , bg = "light grey" ).grid(row = 1 , sticky = W) Label(master, text = "Date :" , bg = "light grey" ).grid(row = 2 , sticky = W) Label(master, text = "Time :" , bg = "light grey" ).grid(row = 3 , sticky = W) Label(master, text = "League :" , bg = "light grey" ).grid(row = 4 , sticky = W) Label(master, text = "Team 1 :" , bg = "light grey" ).grid(row = 5 , sticky = W) Label(master, text = "Team 2 :" , bg = "light grey" ).grid(row = 6 , sticky = W) Label(master, text = "Team 1 score :" , bg = "light grey" ).grid(row = 7 , sticky = W) Label(master, text = "Team 2 score :" , bg = "light grey" ).grid(row = 8 , sticky = W) Label(master, text = "Link :" , bg = "light grey" ).grid(row = 9 , sticky = W) # Creating label for class variable # name using widget Entry Label(master, text = " ", textvariable= date ,bg = " light grey").grid(row = 2 ,column = 1 , sticky = W) Label(master, text = " ", textvariable= time ,bg = " light grey").grid(row = 3 ,column = 1 , sticky = W) Label(master, text = " ", textvariable= league ,bg = " light grey").grid(row = 4 ,column = 1 , sticky = W) Label(master, text = " ", textvariable= team1 ,bg = " light grey").grid(row = 5 ,column = 1 , sticky = W) Label(master, text = " ", textvariable= team2 ,bg = " light grey").grid(row = 6 ,column = 1 , sticky = W) Label(master, text = " ", textvariable= team1_score ,bg = " light grey").grid(row = 7 ,column = 1 , sticky = W) Label(master, text = " ", textvariable= team2_score ,bg = " light grey").grid(row = 8 ,column = 1 , sticky = W) Label(master, text = " ", textvariable= link ,bg = " light grey").grid(row = 9 ,column = 1 , sticky = W) e1 = Entry(master) e1.grid(row = 0 , column = 1 ) e2 = Entry(master) e2.grid(row = 1 , column = 1 ) # creating a button using the widget # Button that will call the submit function b = Button(master, text = "Show" , command = cricket_info ) b.grid(row = 0 , column = 2 ,columnspan = 2 , rowspan = 2 ,padx = 5 , pady = 5 ) mainloop() |
Output: