Получать результаты игры с веб-сайта [closed]

У меня есть код, который очищает футбольные матчи.

Как я могу улучшить свой код?

import os
import pandas as pd
from selenium import webdriver
from tabulate import tabulate
from datetime import datetime
import time
from bs4 import BeautifulSoup as bs 

start = datetime.now()

browser = webdriver.Chrome()


class GameData:

    def __init__(self):
        self.date = []
        self.time = []
        self.game = []
        self.score = []
        self.home_odds = []
        self.draw_odds = []
        self.away_odds = []


def parse_data(url):
    browser.get(url)
    df = pd.read_html(browser.page_source, header=0)[0]
    time.sleep(3)
    html = browser.page_source
    soup = bs(html,"lxml")
    cont = soup.find('div', {'id':'wrap'})
    conti = cont.find('div', {'id':'col-content'})
    content = conti.find('table', {'class':'table-main'}, {'id':'tournamentTable'})
    main = content.find('th', {'class':'first2 tl'})
    count = main.findAll('a')
    country = count[1].text
    league = count[2].text
    game_data = GameData()
    game_date = None
    for row in df.itertuples():
        if not isinstance(row[1], str):
            continue
        elif ':' not in row[1]:
            game_date = row[1].split('-')[0]
            continue
        game_data.date.append(game_date)
        game_data.time.append(row[1])
        game_data.game.append(row[2])
        game_data.score.append(row[3])
        game_data.home_odds.append(row[4])
        game_data.draw_odds.append(row[5])
        game_data.away_odds.append(row[6])
    browser.quit()
    return game_data, country, league

# You can input as many URLs you want
urls = {
"https://www.oddsportal.com/soccer/europe/champions-league/results/"
}

if __name__ == '__main__':

    results = None

    for url in urls:
        game_data, country, competition = parse_data(url)
        result = pd.DataFrame(game_data.__dict__)
        if results is None:
            results = result
        else:
            results = results.append(result, ignore_index=True)


print('The country is',country)
print('The competition is', competition)
print(tabulate(results.head(), headers="keys", tablefmt="github"))
end = datetime.now()
time_taken = end - start
print('Time taken to complete: ', time_taken)

результат:

The country is  Europe
The competition is Champions League
|    | date        | time   | game                                 | score   |   home_odds |   draw_odds |   away_odds |
|----|-------------|--------|--------------------------------------|---------|-------------|-------------|-------------|
|  0 | 17 Mar 2021 | 20:00  | Bayern Munich - Lazio                | 2:1     |        1.35 |        6.02 |        7.53 |
|  1 | 17 Mar 2021 | 20:00  | Chelsea - Atl. Madrid                | 2:0     |        2.33 |        3.1  |        3.49 |
|  2 | 16 Mar 2021 | 20:00  | Manchester City - B. Monchengladbach | 2:0     |        1.28 |        6.25 |       10.17 |
|  3 | 16 Mar 2021 | 20:00  | Real Madrid - Atalanta               | 3:1     |        2.26 |        3.6  |        3.16 |
|  4 | 10 Mar 2021 | 20:00  | Liverpool - RB Leipzig               | 2:0     |        2.37 |        3.8  |        2.85 |
Time taken to complete:  0:00:15.875088

ожидаемый результат:

     date    time       country competition                  game                                     score         home_odds   draw_odds   away_odds
--- --------    --------    --------    --------    --------------------------------------  ---------      ------------ -------------   ------------
0   17/03/2021   20:00      Europe  Champions League     Bayern Munich - Lazio                     2:01         1.35             6.02           7.53
1   17/03/2021   20:00      Europe  Champions League     Chelsea - Atl. Madrid                     2:00         2.33             3.1            3.49
2   16/03/2021   20:00      Europe  Champions League     Manchester City - B. Monchengladbach      2:00         1.28             6.25           10.17
3   16/03/2021   20:00      Europe  Champions League     Real Madrid - Atalanta                    3:01         2.26             3.6            3.16
4   10/03/2021   20:00      Europe  Champions League     Liverpool - RB Leipzig                    2:00         2.37             3.8            2.85

** требуется объяснение с помощью codereview .. Я добавил обновленный фрейм данных, чтобы отразить ожидаемые результаты.

0

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *