Advertisement
Guest User

British Open Day 3

a guest
Jul 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from prettytable import PrettyTable
  4. import operator
  5.  
  6.  
  7. page = requests.get('https://www.bbc.com/sport/golf/major-championships/leaderboard')
  8. soup = BeautifulSoup(page.text, 'html.parser')
  9.  
  10. playerListHTML = soup.select('tr > td > abbr')
  11. playerListAmateurs = []
  12.  
  13. for tag in playerListHTML:
  14.     playerListAmateurs.append(tag['title'])
  15.  
  16. playerList = []
  17. for i in range(len(playerListAmateurs)):
  18.     player = playerListAmateurs[i]
  19.     if player.find('(a)') != -1:
  20.         player = player.replace('(a)','')
  21.         player = player.strip()
  22.     playerList.append(player)
  23.  
  24. scoresETC = soup.select('tbody > tr > td')
  25.  
  26. scoresHTML = []
  27. countriesHTML = []
  28. throughHTML = []
  29.  
  30. for i in range(len(scoresETC)):
  31.     if i % 8 == 1:
  32.         countriesHTML.append(scoresETC[i])
  33.     if i % 8 == 2:
  34.         scoresHTML.append(scoresETC[i])
  35.     if i % 8 == 3:
  36.         throughHTML.append(scoresETC[i])
  37.  
  38. scores = []
  39. for line in scoresHTML:
  40.     scoreStr = line.getText()
  41.     if scoreStr == 'E':
  42.         score = 0
  43.     else:
  44.         score = int(scoreStr)
  45.     scores.append(score)
  46.  
  47. countries = []
  48. for countryLine in countriesHTML:    
  49.     countries.append(countryLine.select('img')[0]['alt'])
  50.    
  51. through = []
  52. for throughLine in throughHTML:
  53.     through.append(throughLine.getText())
  54.  
  55. pickedPlayers = {}
  56.  
  57. def playerLookup(player):
  58.     if player in playerList: # determine cut by no longer being on BBC's website
  59.         loc = playerList.index(player)
  60.     else:
  61.         loc = -1
  62.     return scores[loc]
  63.  
  64. def countryLookup(player):
  65.     loc = playerList.index(player)
  66.     return ' (' + countries[loc] + ')'
  67.  
  68. def throughLookup(player):
  69.     loc = playerList.index(player)
  70.     return through[loc]
  71.  
  72. def teamLookup(team, owner):
  73.     teamScore = []
  74.     for i in range(len(team)):
  75.         player = team[i]
  76.         pickedPlayers.update({player:(owner, playerLookup(player))})
  77.         teamScore.append(playerLookup(player))
  78.     teamScore.sort()
  79.     teamScore.pop(-1)
  80.     score = sum(teamScore)
  81.     return score
  82.  
  83.  
  84. brooks = ['Brooks Koepka', 'Tiger Woods', 'Rory McIlroy', 'Ian Poulter', 'Brandt Snedeker']
  85. payne = ['Justin Rose', 'Jordan Spieth', 'Jason Day', 'Henrik Stenson', 'Louis Oosthuizen']
  86. peart = ['Patrick Reed', 'Dustin Johnson', 'Jon Rahm', 'Justin Thomas', 'Francesco Molinari']
  87. rob = ['Rickie Fowler', 'Alex Noren', 'Tommy Fleetwood', 'Peter Uihlein', 'Sergio Garcia']
  88. jonathan = ['Ryan Moore', 'George Coetzee', 'Gary Woodland', 'Gavin Green', 'Beau Hossler']
  89.  
  90. scoreDict = {'Brooks':(teamLookup(brooks, 'Brooks'), brooks), 'Jonathan': (teamLookup(jonathan, 'Jonathan'), jonathan),
  91.              'Payne':(teamLookup(payne, 'Payne'), payne), 'Peart': (teamLookup(peart, 'Peart'), peart),
  92.              'Rob': (teamLookup(rob, 'Rob'), rob)}
  93.  
  94. # scoreList = [v[0] for k, v in scoreDict.items()] # I think this line actually works instead of the more hardcoded line below, but only if you're
  95.                                                    # programming in Python 3. I'm on Python 2.7. I think the reason is the order items are
  96.                                                    # added to dictionaries is smarter in Python 3.
  97. scoreList = [scoreDict['Brooks'][0], scoreDict['Jonathan'][0], scoreDict['Payne'][0], scoreDict['Peart'][0], scoreDict['Rob'][0]]
  98.  
  99. sortedList = sorted(scoreDict.items(), key = operator.itemgetter(1))
  100.  
  101. print('')
  102. for k in sortedList:
  103.     print(k[0]+':', k[1][0])
  104. # print('')
  105.  
  106. for i in range(len(scoreList)):
  107.     if scoreList[i] >= 0:
  108.         scoreList[i] = ' '+str(scoreList[i])
  109.     else:
  110.         scoreList[i] = str(scoreList[i])
  111.  
  112.  
  113. sorted_pickedPlayers = sorted(pickedPlayers.items(), key = operator.itemgetter(1))
  114.  
  115. scorePlayers = PrettyTable()
  116. scorePlayers.field_names = [ "Top 4", "Owner", "Player", "Score", "Through"]
  117. scorePlayers.align["Score"] = "r"
  118. scorePlayers.align["Through"] = "r"
  119.  
  120.  
  121. prevOwner = sorted_pickedPlayers[0][1][0]
  122. ownerNumber = 0
  123. for dataline in sorted_pickedPlayers:
  124.     if dataline[0] in playerList: # determine cut by no longer being on BBC's website
  125.         cut = ''
  126.     else:
  127.         cut = 'CUT'
  128.     if cut == 'CUT':
  129. #         throughVal = 'F'
  130.         throughVal1 = through[-1]
  131.         throughVal2 = min(through)
  132.         throughVal = throughVal2+' ('+str(throughVal1)+')'
  133.         player = '---- CUT ----'
  134.         countryCode = ''
  135.         scoreVal = scores[-1]
  136.     else:
  137.         player = dataline[0]
  138.         countryCode = countryLookup(dataline[0])
  139.         throughVal = str(throughLookup(dataline[0]))+' '
  140.         scoreVal = dataline[1][1]
  141.    
  142.     currentOwner = dataline[1][0]
  143.     if ownerNumber == 0:
  144.         scorePlayers.add_row([scoreList[ownerNumber]+' ', dataline[1][0], player+countryCode, scoreVal, throughVal])
  145.         ownerNumber+=1
  146.     elif currentOwner != prevOwner:
  147.         scorePlayers.add_row(['','','','',''])
  148.         scorePlayers.add_row([scoreList[ownerNumber]+' ', dataline[1][0], player+countryCode, scoreVal, throughVal])
  149.         ownerNumber+=1
  150.     else:
  151.         scorePlayers.add_row(['',dataline[1][0], player+countryCode, scoreVal, throughVal])
  152.     prevOwner = currentOwner
  153.  
  154. print(scorePlayers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement