如何抓取足球周结果

问题描述 投票:0回答:2

我想获得虚拟足球联赛的结果,并根据此 excel 文件中所示的周进行排列,我正在尝试从该网站抓取结果 https://odibets.com/league?br= 1&tab=results 下面是迄今为止编写的Python代码

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)

我如何编写代码才能达到与excel文件中所示相同的结果

python html web-scraping beautifulsoup
2个回答
1
投票

此脚本将打印周、团队和分数:

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))

打印:

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.

0
投票

导入请求 从 bs4 导入 BeautifulSoup

url = 'https://odibets.com/league?br=1&tab=results' 汤 = BeautifulSoup(requests.get(url).content, 'html.parser')

打印('{:<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}'.

© www.soinside.com 2019 - 2024. All rights reserved.