Advertisement
steve-shambles-2109

Scrape Football Premier League Table

Nov 4th, 2019
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. """
  2. Python code snippets vol 40.
  3. xxx-Scrape Football Premier League Table.
  4.  
  5. By Steve Shambles Nov 2019, with help from r/learnpython/reddit.
  6. stevepython.wordpress.com
  7.  
  8. pip install BeautifulSoup
  9. pip install requests
  10. """
  11.  
  12. import requests
  13. from bs4 import BeautifulSoup
  14.  
  15. r = requests.get('https://www.skysports.com/premier-league-table')
  16. soup = BeautifulSoup(r.text, 'html.parser')
  17. league_table = soup.find('table', class_='standing-table__table callfn')
  18.  
  19. print("Team                         Pl  W  D  L  F  A GD Pts")
  20.  
  21. for team in league_table.find_all('tbody'):
  22.     rows = team.find_all('tr')
  23.  
  24.     for row in rows:
  25.         rank = row.find_all('td', class_='standing-table__cell')[0].text
  26.         pl_team = row.find('td', class_='standing-table__cell standing-table__cell--name')
  27.         pl_team = pl_team['data-long-name']
  28.         games_played = row.find_all('td', class_='standing-table__cell')[2].text
  29.         games_won = row.find_all('td', class_='standing-table__cell')[3].text
  30.         games_drawn = row.find_all('td', class_='standing-table__cell')[4].text
  31.         games_lost = row.find_all('td', class_='standing-table__cell')[5].text
  32.         goals_for = row.find_all('td', class_='standing-table__cell')[6].text
  33.         goals_against = row.find_all('td', class_='standing-table__cell')[7].text
  34.         goal_diff = row.find_all('td', class_='standing-table__cell')[8].text
  35.         total_points = row.find_all('td', class_='standing-table__cell')[9].text
  36.  
  37.        
  38.         # Need 25 spaces after pl_team to format table for printing.
  39.         space_pad = 25 - len(pl_team)
  40.         pl_team = pl_team + " " * space_pad
  41.  
  42.         # If single figure, pad with a space to format table.
  43.         if int(games_won) < 10:
  44.             games_won = " "+str(games_won)
  45.  
  46.         if int(games_drawn) < 10:
  47.             games_drawn = " "+str(games_drawn)
  48.  
  49.         if int(games_lost) < 10:
  50.             games_lost = " "+str(games_lost)
  51.  
  52.         if int(goals_for) < 10:
  53.             goals_for = " "+str(goals_for)
  54.  
  55.         if int(goals_against) < 10:
  56.             goals_against = " "+str(goals_against)
  57.  
  58.         if int(goal_diff) < 10 and int(goal_diff) >= 0:
  59.             goal_diff = " "+str(goal_diff)
  60.  
  61.         if int(rank) < 10:
  62.             rank = " "+str(rank)
  63.  
  64.         # Print table in shell.
  65.         print(rank, pl_team, games_played, games_won, games_drawn, games_lost,
  66.               goals_for, goals_against, goal_diff, total_points)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement