1
\$\begingroup\$

A member of SO who I immensely respect just told me that the code below makes him uncomfortable.

class GameData:

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


def parse_data(url):
    while True:
        try:
            browser.get(url)
            df = pd.read_html(browser.page_source)[0]
            break
        except KeyError:
            browser.quit()
            continue
    soup = bs(html, "lxml")
    cont = soup.find('div', {'id': 'wrap'})
    content = cont.find('div', {'id': 'col-content'})
    content = content.find('table', {'class': 'table-main'}, {'id': 'tournamentTable'})
    main = content.find('th', {'class': 'first2 tl'})
    if main is None:
        return None
    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])
        game_data.country.append(country)
        game_data.league.append(league)
    return game_data


urls = {
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/2/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/3/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/4/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/5/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/6/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/7/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/8/",
"https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/9/",

}

if __name__ == '__main__':

    results = None

    for url in urls:
        try:
            game_data = parse_data(url)
            if game_data is None:
                continue
            result = pd.DataFrame(game_data.__dict__)
            if results is None:
                results = result
            else:
                results = results.append(result, ignore_index=True)
        except ValueError:
            game_data = parse_data(url)
            if game_data is None:
                continue
            result = pd.DataFrame(game_data.__dict__)
            if results is None:
                results = result
            else:
                results = results.append(result, ignore_index=True)
        except AttributeError:
            game_data = parse_data(url)
            if game_data is None:
                continue
            result = pd.DataFrame(game_data.__dict__)
            if results is None:
                results = result
            else:
                results = results.append(result, ignore_index=True)

While I admit, this code has been stitched together from solutions from SO, how can I improve the code for effeciency, redability and logic?

\$\endgroup\$
6
  • 2
    \$\begingroup\$ This seems like a repeat of codereview.stackexchange.com/questions/263390/… . Have you read the answer there? \$\endgroup\$ Commented Jul 7, 2021 at 1:38
  • \$\begingroup\$ Yes, This is a repeat question. I had worked on improving the code however, I had so many errors and I am not a great developer I admit! The code works as it is; I was hoping if the community can code it better. Apologies for the repeat however I hope you understand why I have posted it again. \$\endgroup\$ Commented Jul 7, 2021 at 1:54
  • 1
    \$\begingroup\$ I mean... the community isn't specifically for coding it better, but rather telling you how to code it better. Unless you vote one way or another on the answer to your first question I have no evidence that you're actually reading what I write. Please also read through codereview.stackexchange.com/questions/263433/… . \$\endgroup\$ Commented Jul 7, 2021 at 2:55
  • \$\begingroup\$ You are correct! However, I will have to learn quite a bit of platforms to really digest the code posted there. Can you help improve the code using mostly pandas and beautifulsoup as I seem to digest these platforms with relativel less effort and investment. Though I admit, I should be on top of the code, my apologies for not being so. \$\endgroup\$ Commented Jul 7, 2021 at 3:23
  • \$\begingroup\$ The methods you have are quite big, code really needs to flow, like a story in a book or a poem... it's an art. The goal is to make it simple enough for any english speaking individual to understand what it is about. I tend to ask my self: what will my partner say about my code? \$\endgroup\$ Commented Jul 7, 2021 at 7:53

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.