I am very new to web scraping and I am trying different ways to run this code that works with the same tabular scraping on the same website (different URL though) but I am getting nowhere.
working code:
browser = webdriver.Chrome()
urls = {
"https://www.oddsportal.com/soccer/england/premier-league/results/"
}
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):
browser.get(url)
df = pd.read_html(browser.page_source, header=0)[0]
html = browser.page_source
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
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
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)
df:
| | date | time | game | score | home_odds | draw_odds | away_odds | country | league |
|----|-------------------|--------|----------------------------------|---------|-------------|-------------|-------------|-----------|----------------|
| 0 | Yesterday, 11 May | 19:15 | Southampton - Crystal Palace | 3:1 | 1.89 | 3.8 | 4.11 | England | Premier League |
| 1 | Yesterday, 11 May | 17:00 | Manchester Utd - Leicester | 1:2 | 3.72 | 3.58 | 2.07 | England | Premier League |
| 2 | 10 May 2021 | 19:00 | Fulham - Burnley | 0:2 | 2.24 | 3.44 | 3.38 | England | Premier League |
| 3 | 09 May 2021 | 18:00 | Arsenal - West Brom | 3:1 | 1.5 | 4.53 | 6.76 | England | Premier League |
| 4 | 09 May 2021 | 15:30 | West Ham - Everton | 0:1 | 2.15 | 3.56 | 3.48 | England | Premier League |
Here's what I have found as far as differences go:
Xpath
of working code url:
urls = {
"https://www.oddsportal.com/soccer/england/premier-league/results/"
}
//*[@id="tournamentTable"]/tbody/tr[4]/td[2]/a
Xpath
of desired url:
urls = {
"https://www.oddsportal.com/matches/soccer/20210515/"
}
//*[@id="table-matches"]/table/tbody/tr[2]/td[2]/a[2]
When I run
if main is None:
return None
count = main.findAll('a')
print(len(count))
I get
2
I had asked this question before and tried
content = cont.find('div', {'id': 'col-content'})
content = content.find('table', {'class': 'table-main'}, {'id': 'table-matches'})
main = content.find('th', {'class': 'first2 tl'})
However I am more confused than before.
Any guidance will be much appreciated.