Search code examples
pythonhtmlweb-scrapingbeautifulsoup

How to scrape football week results


I want to get the results of a virtual football league and arrange them according to the weeks as shown in this excel file, am trying to scrape the results from this site https://odibets.com/league?br=1&tab=results and below is the python code have written so far

import requests
from bs4 import BeautifulSoup
import csv
import pandas as pd

page = requests.get('https://odibets.com/league?br=1&tab=results')
soup = BeautifulSoup(page.text, 'html.parser')
table = soup.find(class_="l-league-table-results" )

results = table.find_all(class_='results')
for i in results:
        weeks = i.find(class_='results-title').getText()
        week = weeks[55:58]
        

        day = i.find_all(class_='results-body')[3]
        home = day.find_all('td')[0].getText()
        away = day.find_all('td')[2].getText()
        score = day.find_all('td')[1].getText()
        homeScore = score[:1]
        awayScore = score[1:]
        print(awayScore)

How can i write the code to achieve the same results as shown in the excel file


Solution

  • This script will print week, teams and scores:

    import requests
    from bs4 import BeautifulSoup
    
    
    url = 'https://odibets.com/league?br=1&tab=results'
    soup = BeautifulSoup(requests.get(url).content, 'html.parser')
    
    print('{:<5}{:<25}{:<25}{:<12}{:<12}'.format('WEEK', 'HOME', 'AWAY', 'HOME SCORE', 'AWAY SCORE'))
    for row in soup.select('tr.results-body'):
        week = row.find_previous('td', colspan="3").text.split()[3]
        home_team, home_score, away_score, away_team = row.get_text(strip=True, separator='|').split('|')
        print('{:<5}{:<25}{:<25}{:<12}{:<12}'.format(week, home_team, away_team, home_score, away_score))
    

    Prints:

    WEEK HOME                     AWAY                     HOME SCORE  AWAY SCORE  
    34   NORWICH                  Burnley                  1           1           
    34   Manchester Reds          Wolves                   2           2           
    34   SHEFFIELD U              Liverpool                0           1           
    34   ASTON V                  Brighton                 1           2           
    34   London Reds              Leicester                2           3           
    34   Tottenham                Bournemouth              0           0           
    34   Newcastle                Manchester Blue          1           4           
    34   Southampton              Palace                   2           0           
    34   West Ham                 Watford                  0           0           
    34   London Blues             Everton                  3           1           
    33   Bournemouth              NORWICH                  1           0           
    33   Watford                  London Blues             0           0           
    33   Wolves                   ASTON V                  4           0           
    33   Burnley                  SHEFFIELD U              2           1           
    33   Palace                   London Reds              0           1           
    33   Brighton                 West Ham                 0           0           
    33   Manchester Reds          Tottenham                1           4           
    33   Everton                  Newcastle                2           1           
    33   Leicester                Manchester Blue          1           3           
    33   Liverpool                Southampton              1           0           
    32   Southampton              Burnley                  3           2           
    32   West Ham                 Wolves                   0           2           
    32   London Blues             Brighton                 0           2           
    32   Tottenham                NORWICH                  1           0           
    
    ...and so on.