Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- from prettytable import PrettyTable
- import operator
- page = requests.get('https://www.bbc.com/sport/golf/major-championships/leaderboard')
- soup = BeautifulSoup(page.text, 'html.parser')
- playerListHTML = soup.select('tr > td > abbr')
- playerListAmateurs = []
- for tag in playerListHTML:
- playerListAmateurs.append(tag['title'])
- playerList = []
- for i in range(len(playerListAmateurs)):
- player = playerListAmateurs[i]
- if player.find('(a)') != -1:
- player = player.replace('(a)','')
- player = player.strip()
- playerList.append(player)
- scoresETC = soup.select('tbody > tr > td')
- scoresHTML = []
- countriesHTML = []
- throughHTML = []
- for i in range(len(scoresETC)):
- if i % 8 == 1:
- countriesHTML.append(scoresETC[i])
- if i % 8 == 2:
- scoresHTML.append(scoresETC[i])
- if i % 8 == 3:
- throughHTML.append(scoresETC[i])
- scores = []
- for line in scoresHTML:
- scoreStr = line.getText()
- if scoreStr == 'E':
- score = 0
- else:
- score = int(scoreStr)
- scores.append(score)
- countries = []
- for countryLine in countriesHTML:
- countries.append(countryLine.select('img')[0]['alt'])
- through = []
- for throughLine in throughHTML:
- through.append(throughLine.getText())
- pickedPlayers = {}
- def playerLookup(player):
- if player in playerList: # determine cut by no longer being on BBC's website
- loc = playerList.index(player)
- else:
- loc = -1
- return scores[loc]
- def countryLookup(player):
- loc = playerList.index(player)
- return ' (' + countries[loc] + ')'
- def throughLookup(player):
- loc = playerList.index(player)
- return through[loc]
- def teamLookup(team, owner):
- teamScore = []
- for i in range(len(team)):
- player = team[i]
- pickedPlayers.update({player:(owner, playerLookup(player))})
- teamScore.append(playerLookup(player))
- teamScore.sort()
- teamScore.pop(-1)
- score = sum(teamScore)
- return score
- brooks = ['Brooks Koepka', 'Tiger Woods', 'Rory McIlroy', 'Ian Poulter', 'Brandt Snedeker']
- payne = ['Justin Rose', 'Jordan Spieth', 'Jason Day', 'Henrik Stenson', 'Louis Oosthuizen']
- peart = ['Patrick Reed', 'Dustin Johnson', 'Jon Rahm', 'Justin Thomas', 'Francesco Molinari']
- rob = ['Rickie Fowler', 'Alex Noren', 'Tommy Fleetwood', 'Peter Uihlein', 'Sergio Garcia']
- jonathan = ['Ryan Moore', 'George Coetzee', 'Gary Woodland', 'Gavin Green', 'Beau Hossler']
- scoreDict = {'Brooks':(teamLookup(brooks, 'Brooks'), brooks), 'Jonathan': (teamLookup(jonathan, 'Jonathan'), jonathan),
- 'Payne':(teamLookup(payne, 'Payne'), payne), 'Peart': (teamLookup(peart, 'Peart'), peart),
- 'Rob': (teamLookup(rob, 'Rob'), rob)}
- # 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
- # programming in Python 3. I'm on Python 2.7. I think the reason is the order items are
- # added to dictionaries is smarter in Python 3.
- scoreList = [scoreDict['Brooks'][0], scoreDict['Jonathan'][0], scoreDict['Payne'][0], scoreDict['Peart'][0], scoreDict['Rob'][0]]
- sortedList = sorted(scoreDict.items(), key = operator.itemgetter(1))
- print('')
- for k in sortedList:
- print(k[0]+':', k[1][0])
- # print('')
- for i in range(len(scoreList)):
- if scoreList[i] >= 0:
- scoreList[i] = ' '+str(scoreList[i])
- else:
- scoreList[i] = str(scoreList[i])
- sorted_pickedPlayers = sorted(pickedPlayers.items(), key = operator.itemgetter(1))
- scorePlayers = PrettyTable()
- scorePlayers.field_names = [ "Top 4", "Owner", "Player", "Score", "Through"]
- scorePlayers.align["Score"] = "r"
- scorePlayers.align["Through"] = "r"
- prevOwner = sorted_pickedPlayers[0][1][0]
- ownerNumber = 0
- for dataline in sorted_pickedPlayers:
- if dataline[0] in playerList: # determine cut by no longer being on BBC's website
- cut = ''
- else:
- cut = 'CUT'
- if cut == 'CUT':
- # throughVal = 'F'
- throughVal1 = through[-1]
- throughVal2 = min(through)
- throughVal = throughVal2+' ('+str(throughVal1)+')'
- player = '---- CUT ----'
- countryCode = ''
- scoreVal = scores[-1]
- else:
- player = dataline[0]
- countryCode = countryLookup(dataline[0])
- throughVal = str(throughLookup(dataline[0]))+' '
- scoreVal = dataline[1][1]
- currentOwner = dataline[1][0]
- if ownerNumber == 0:
- scorePlayers.add_row([scoreList[ownerNumber]+' ', dataline[1][0], player+countryCode, scoreVal, throughVal])
- ownerNumber+=1
- elif currentOwner != prevOwner:
- scorePlayers.add_row(['','','','',''])
- scorePlayers.add_row([scoreList[ownerNumber]+' ', dataline[1][0], player+countryCode, scoreVal, throughVal])
- ownerNumber+=1
- else:
- scorePlayers.add_row(['',dataline[1][0], player+countryCode, scoreVal, throughVal])
- prevOwner = currentOwner
- print(scorePlayers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement