steve-shambles-2109

197-Live Football Score Checker

Nov 9th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.60 KB | None | 0 0
  1. """
  2. Python code snippets vol 40.
  3. 197-Live Football Score Checker
  4. stevepython.wordpress.com
  5.  
  6. pip install BeautifulSoup
  7. pip install requests
  8. pip install lxml
  9.  
  10. source:
  11. https://github.com/utting98/football-score-checker/blob/master/FootballScoreChecker.py
  12. """
  13.  
  14. import requests
  15. from bs4 import BeautifulSoup
  16. from datetime import date
  17.  
  18. def main(teamlist):
  19.     datetoday = date.today() #get current date to add to url
  20.     url = 'https://www.bbc.co.uk/sport/football/scores-fixtures/'+str(datetoday) #specify url of page for today
  21.     result = requests.get(url) #get access to the page via python
  22.     soup = BeautifulSoup(result.content, "lxml") #store the page html as a variable that we are going to look at the tags of with BeautifulSoup
  23.     #look for all of the spans with the attribute class defined by the long tag the home team name is stored under
  24.     teams = soup.find_all("span",attrs={'class':"gs-u-display-none gs-u-display-block@m qa-full-team-name sp-c-fixture__team-name-trunc"})
  25.     finishedhomescores = soup.find_all("span",attrs={'class':"sp-c-fixture__number sp-c-fixture__number--home sp-c-fixture__number--ft"})
  26.     livehomescores = soup.find_all("span",attrs={'class':"sp-c-fixture__number sp-c-fixture__number--home sp-c-fixture__number--live"})
  27.     finishedawayscores = soup.find_all("span",attrs={'class':"sp-c-fixture__number sp-c-fixture__number--away sp-c-fixture__number--ft"})
  28.     liveawayscores = soup.find_all("span",attrs={'class':"sp-c-fixture__number sp-c-fixture__number--away sp-c-fixture__number--live"})
  29.     matchstatus = soup.find_all("span",attrs={'class':"sp-c-fixture__status-wrapper"})
  30.     homenames = [] #define empty list to fill with the actual names, before we just had tag info
  31.     awaynames = []
  32.     homescores = []
  33.     awayscores = []
  34.     #this will need updating yearly, can replace this list with any teams you are interested in following and will only get scores for those teams
  35.     if(teamlist=='Prem'):
  36.         premteamslist = ['Arsenal','AFC Bournemouth','Aston Villa','Brighton & Hove Albion','Burnley','Chelsea','Crystal Palace','Everton','Leicester City','Liverpool','Manchester City','Manchester United','Newcastle United','Norwich City','Sheffield United','Southampton','Tottenham Hotspur','Watford','West Ham United','Wolverhampton Wanderers']
  37.     else:
  38.         premteamslist = teamlist
  39.     premhomescores = []
  40.     premawayscores = []
  41.     matchtimes = []
  42.     premmatchtimes = []
  43.     finishediterator = 0
  44.     liveiterator=0
  45.  
  46.     for j in range(0,len(matchstatus)):
  47.         if(matchstatus[j].text=='FT'): #if full time append full time score
  48.             homescores.append(finishedhomescores[finishediterator].text)
  49.             awayscores.append(finishedawayscores[finishediterator].text)
  50.             matchtimes.append(matchstatus[j].text)
  51.             finishediterator+=1 #increase finished iterator so it only goes up when a finished score is appended
  52.         elif(('mins' in matchstatus[j].text) or (matchstatus[j].text=='HT') or ('+' in matchstatus[j].text)): #if it says a minute count game is live so append live score
  53.             homescores.append(livehomescores[liveiterator].text)
  54.             awayscores.append(liveawayscores[liveiterator].text)
  55.             matchtimes.append(matchstatus[j].text)
  56.             liveiterator+=1 #as with the finished iterator
  57.         else: #if it is blank a game today hasn't kicked off yet so flag for deletion (can remove this to still display something else but score may have issues)
  58.             homescores.append('N/A') #An easy to spot flag, means game postponed or not kicked off yet
  59.             awayscores.append('N/A')
  60.             matchtimes.append('N/A') #Flag game time not running yet as not kicked off
  61.  
  62.     premflag = [0]*len(matchstatus) #list of flags to note if prem game
  63.  
  64.     for i in range(0,len(teams),2): #loop over to do all of the found teams in incremenets of two to stop duplicates
  65.  
  66.         if(i>0): #also loop an iterator increasing by 1
  67.             j=int(i/2)
  68.         else:
  69.             j=0
  70.  
  71.         for k in range(0,len(premteamslist)): #loop over premteamsllist
  72.             if(teams[i].text==premteamslist[k]): #if teams is in premteamslist
  73.                 if((teams[i].text in homenames) or (teams[i].text in awaynames)): #and name not already listed (this is to stop duplicates e.g. getting women's teams when you wanted mens etc. or vice versa)
  74.                     pass
  75.                 else:
  76.                     homenames.append(teams[i].text) #append the text inside the tag for home teams, e.g. AFC Bournemouth
  77.                     awaynames.append(teams[i+1].text)
  78.                     premmatchtimes.append(matchtimes[j])
  79.                     premflag[j]=1 #flag as prem team
  80.             else:
  81.                 pass
  82.         l = i+1 #also check odd indexes for away teams
  83.         for k in range(0,len(premteamslist)): #loop over the same way
  84.             if(teams[l].text==premteamslist[k]): #if the away team is in premteamslist
  85.                 if((teams[l].text in homenames) or (teams[l].text in awaynames)): #and game isn't already found from home teams list
  86.                     pass
  87.                 else:
  88.                     #not the indexes are swapped because here we've gone past the home to find the away so previous index is the home team
  89.                     homenames.append(teams[l-1].text) #append the text inside the tag for home teams, e.g. AFC Bournemouth
  90.                     awaynames.append(teams[l].text)
  91.                     premmatchtimes.append(matchtimes[j])
  92.                     premflag[j]=1 #flag as prem team
  93.             else:
  94.                 pass
  95.  
  96.     for i in range(0,len(homescores)): #loop over all appended homescores (now a full list with just 'Del' if postponed or cancelled)
  97.         if(premflag[i]==1): #if game was prem
  98.             premhomescores.append(homescores[i]) #append the scores
  99.             premawayscores.append(awayscores[i])
  100.  
  101.     gamestrings = [] #empty gamestring list
  102.     for i in range(0,len(homenames)): #loop over and define score string for game then display them
  103.         gamestrings.append(homenames[i]+' '+premhomescores[i] + ' - ' + premawayscores[i] + ' ' + awaynames[i])
  104.  
  105.     return gamestrings,premmatchtimes
  106.  
  107. if __name__=='__main__':
  108.     #main function call, if you want all premier league scores pass argument 'Prem' else pass a list of teams such as ['Leicester City','Manchester United'] etc exactly as they appear on BBC Sport fixtures and scores
  109.     #not still pass in list notation even if it is a single team e.g. ['Leicester City']
  110.     matches,matchtimes = main('Prem')
  111.     for i in range(0,len(matches)):
  112.         print(matches[i])
  113.         print(matchtimes[i])
Add Comment
Please, Sign In to add comment