-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathScrapeOddsportal.py
407 lines (308 loc) · 15.1 KB
/
ScrapeOddsportal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from datetime import datetime
from utils.Features import *
import numpy as np
from pathlib import Path
import pandas as pd
import time
import re
import os
from utils.OddsData import *
DRIVER_PATH = "chromedriver"
# DRIVER_PATH = Path('./chromedriver/chromedriver.exe').absolute()
def login(driver):
login_url = 'https://www.oddsportal.com/login/'
wait = WebDriverWait(driver, 30)
driver.get(login_url)
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='login-username1']"))).send_keys("Derik1337")
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='login-password1']"))).send_keys("D4Cit.W9iC3K4iK")
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))).click()
print('-----Logged in!-----')
return driver
def extract_open_close_odds(driver, data):
if len(data.text) <= 4:
close = float(data.text)
else:
close = float(data.text.split('\n')[0])
try:
ActionChains(driver).move_to_element(data).perform()
if len(data.text) > 4:
time.sleep(0.5)
odds_data = data.find_element_by_xpath("//*[@id='tooltiptext']")
odds_data = odds_data.get_attribute("innerHTML").split('Opening odds:')[1].replace(
'<strong>Click to BET NOW</strong>', '').replace('<br>', '')
open_date = odds_data[0:13]
if 'Dec' in open_date and datetime.today().month == 1:
open_date = datetime.strptime(open_date + ', ' + str(datetime.today().year - 1), '%d %b, %H:%M, %Y')
else:
open_date = datetime.strptime(open_date + ', ' + str(datetime.today().year), '%d %b, %H:%M, %Y')
odds = odds_data[13:]
open = float(re.search('<strong>(.*)</strong>', odds).group(1))
except:
print('No Open odds found (ERROR line ~45)!')
open = close
return open, close
def strip_day(date_str):
return date_str.replace('Monday, ', '').replace('Tuesday, ', '').replace('Wednesday, ', '').replace('Thursday, ',
'').replace(
'Friday, ', '').replace('Saturday, ', '').replace('Sunday, ', '').replace('Yesterday, ', '').replace('Today, ',
'').replace(
'Tomorrow, ', '')
def get_meta_data(driver, country, league, results=True):
# time.sleep(1)
teams = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@id =" + '"col-content"' + "]/h1"))).text.split(' - ')
H_team, A_team = teams[0].replace(' ', ''), teams[1].replace(' ', '')
# time.sleep(1)
date_str = strip_day(WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@id =" + '"col-content"' + "]/p"))).text)
date = datetime.strptime(date_str, '%d %b %Y, %H:%M')
if results:
results = driver.find_element_by_xpath("//*[@id =" + '"col-content"' + "]/div/p").text.replace('Final result ',
'').replace('(',
'').replace(
')', '').replace(',', '').split(' ')
FTHG, FTAG = int(results[0].split(':')[0]), int(results[0].split(':')[1])
HT1HG, HT1AG = int(results[1].split(':')[0]), int(results[1].split(':')[1])
HT2HG, HT2AG = int(results[2].split(':')[0]), int(results[2].split(':')[1])
dict = {'country': [country], 'league': [league], 'h_team': [H_team],
'a_team': [A_team], 'date': [date.date()], 'time': [date.time()],
'fthg': [FTHG], 'ftag': [FTAG], 'ht1hg': [HT1HG], 'ht1ag': [HT1AG],
'ht2hg': [HT2HG], 'ht2ag': [HT2AG]}
else:
dict = {'country': [country], 'league': [league], 'h_team': [H_team],
'a_team': [A_team], 'date': [date.date()], 'time': [date.time()]}
# PRINTS
# print(H_team + ' - ' + A_team)
# print(datetime_obj)
# print(FTHG + ':' + FTAG + ' (' + HT1HG + ':' + HT1AG + ', ' + HT2HG + ':' + HT2AG + ')')
return dict
def complete_dict(driver, dict, bookie, home, draw, away):
b = bookie
H_open, H_close = extract_open_close_odds(driver, home)
D_open, D_close = extract_open_close_odds(driver, draw)
A_open, A_close = extract_open_close_odds(driver, away)
new_dict = {'HO_' + b: [H_open], 'DO_' + b: [D_open], 'AO_' + b: [A_open],
'HC_' + b: [H_close], 'DC_' + b: [D_close], 'AC_' + b: [A_close]}
dict.update(new_dict)
return dict
def scrape_match_page(driver, country, league, results=True):
dict = get_meta_data(driver, country, league, results)
odds_data_table = driver.find_elements_by_xpath("//*[@id =" + '"odds-data-table"' + "]/div[1]/table/tbody/tr")
num_rows = len(odds_data_table)
print('Reading odds for ' + str(dict['h_team'][0]) + ' - ' + str(dict['a_team'][0]) + '...')
for index in (range(1, num_rows)):
row_data = driver.find_elements_by_xpath(
"//*[@id =" + '"odds-data-table"' + "]/div[1]/table/tbody/tr[" + str(index) + "]/td")
bookie, home, draw, away = row_data[0].text.replace(' ', ''), row_data[1], row_data[2], row_data[3]
dict = complete_dict(driver, dict, bookie, home, draw, away)
if results:
exch_odds_data_table = driver.find_elements_by_xpath(
"//*[@id =" + '"odds-data-table"' + "]/div[3]/table/tbody/tr")
num_rows = len(exch_odds_data_table)
# time.sleep(2)
for index in (range(1, num_rows)):
# print('exchange#:', index)
row_data = driver.find_elements_by_xpath(
"//*[@id =" + '"odds-data-table"' + "]/div[3]/table/tbody/tr[" + str(index) + "]/td")
bookie, home2, draw2, away2 = row_data[0].text.replace(' ', ''), row_data[2], row_data[3], row_data[4]
dict = complete_dict(driver, dict, bookie, home2, draw2, away2)
df = pd.DataFrame(dict)
return df
def scrape_matches(driver, match_urls, country, league, stop_at=None, results=True):
df = pd.DataFrame()
for url in match_urls:
driver.get(url)
try:
meta_data = get_meta_data(driver, country, league, results)
# If we are scraping historic results
if results:
# Check that the match is in the past
if meta_data['date'][0] < datetime.now().date():
match_df = scrape_match_page(driver, country, league, results)
# If stop_at is not None, but a DF
if isinstance(stop_at, pd.DataFrame):
# If we found the match we should stop at
if (match_df['h_team'].values[0] == stop_at['h_team'].values[0]) and \
(match_df['a_team'].values[0] == stop_at['a_team'].values[0]) and \
(str(match_df['date'].values[0]) == str(stop_at['date'].values[0])):
return df, True
# If we are scraping upcoming matches
else:
match_df = scrape_match_page(driver, country, league, results)
df = pd.concat([match_df, df], sort=False, ignore_index=True).fillna(np.nan)
except Exception as e:
print('Could not scrape match:', str(url))
return df, False
def get_page_urls(driver):
# time.sleep(1)
years_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[@class =" + '"main-menu2 main-menu-gray"' + "]")))
links = years_element.find_elements_by_tag_name("a")
urls = []
for link in (links):
urls.append(str(link.get_attribute('href')))
return urls
def get_match_urls(driver, results=True):
# time.sleep(2)
if results:
tournamentTable = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@id =" + '"tournamentTable"' + "]/table/tbody")))
else:
tournamentTable = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//*[@id =" + '"tournamentTable"' + "]")))
links = tournamentTable.find_elements_by_tag_name("a")
urls = []
for link in (links):
link_str = str(link.get_attribute('href'))
if link_str.count('/') > 6:
urls.append(link_str)
return urls
def get_sub_page_urls(driver):
try:
tournamentTable = driver.find_element_by_xpath("//*[@id =" + '"tournamentTable"' + "]/div[1]")
links = tournamentTable.find_elements_by_tag_name("a")
urls = []
for link in (links):
if link.get_attribute('href') not in urls:
urls.append(link.get_attribute('href'))
return urls
except NoSuchElementException:
return [driver.current_url]
def file_exists(country, league):
paths = get_all_paths()
for path in paths:
if (country + '/' + league) in path:
return True
return False
def get_path(country, league):
paths = get_all_paths()
for path in paths:
if (country + '/' + league) in path:
return path
return None
def get_all_countries_and_leagues():
paths = get_all_paths()
countries_and_leagues = []
for path in paths:
country_and_league = path.split('/')[-2:]
country_and_league[1] = country_and_league[1].split('_')[0]
countries_and_leagues.append(country_and_league)
return countries_and_leagues
def update_league_results(country, league):
old_df = read_odds(countries=country, leagues=league)
latest_match_df = old_df[-1:]
print('Stopping at:', latest_match_df['H_team'], latest_match_df['A_team'], latest_match_df['Date'])
new_df = scrape_historical_league(country, league, stop_at=latest_match_df)
new_df = pd.concat([old_df, new_df], sort=False, ignore_index=True).fillna(np.nan)
new_df = drop_duplicates(new_df)
base_path = './data/soccer/historical/' + country + '/'
file_name = league + '_' + str(datetime.now().date()) + '.csv'
new_df.to_csv(os.path.join(base_path, file_name))
def update_all_leagues():
countries_and_leagues = get_all_countries_and_leagues()
for [country, league] in countries_and_leagues:
print('Updating results for:', country, league)
update_league_results(country, league)
def scrape_upcoming_matches(country='england', league='premier-league'):
driver = get_driver()
driver = login(driver)
url = "https://www.oddsportal.com/soccer/" + country + "/" + league + "/"
driver.get(url)
match_urls = get_match_urls(driver, results=False)
matches_df, stop = scrape_matches(driver, match_urls, country, league, stop_at=None, results=False)
driver.quit()
return matches_df
def combine_upcoming_and_old(upcoming, country, league, old_df):
old_df["date"] = [pd.to_datetime(x).date() for x in old_df["date"]]
df = pd.concat([old_df, upcoming], sort=False, ignore_index=True).fillna(np.nan)
df = transform_odds_to_probs(df)
df = drop_duplicates(df)
df = drop_bookies(df)
df = sort_odds_by_date(df)
return df
def scrape_historical_league(country, league, stop_at=None):
base_path = './data/soccer/historical/' + country + '/'
file_name = league + '_' + str(datetime.now().date()) + '.csv'
if not os.path.exists(base_path):
os.makedirs(base_path)
driver = get_driver()
driver = login(driver)
start_url = 'https://www.oddsportal.com/soccer/' + country + '/' + league + '/results/'
driver.get(start_url)
page_urls = get_page_urls(driver)
df = pd.DataFrame()
for index1, url in enumerate(page_urls):
if index1 != 0:
driver.get(url)
sub_pages = get_sub_page_urls(driver)
for index2, sub_url in enumerate(sub_pages):
print('-------Scraping page ' + str(index1 + 1) + '.' + str(index2 + 1) + '-------')
print(sub_url)
if index2 != 0:
driver.get(sub_url)
match_urls = get_match_urls(driver)
matches_df, stop = scrape_matches(driver, match_urls, country, league, stop_at)
df = pd.concat([matches_df, df], sort=False, ignore_index=True).fillna(np.nan)
if stop:
return df
else:
df.to_csv(os.path.join(base_path, file_name))
driver.quit()
return df
def get_driver():
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-dev-shm-usage')
options.headless = True
options.add_argument('--window-size=2560,1400')
options.add_argument('log-level=1')
driver = webdriver.Chrome(DRIVER_PATH, options=options)
return driver
def scrape_upcoming(country, league, existing_matches):
existing_matches = existing_matches[existing_matches['league'] == league]
existing_matches = existing_matches[existing_matches['country'] == country]
upcoming_matches_df = scrape_upcoming_matches(country, league)
print(upcoming_matches_df)
# upcoming_matches_df.to_csv("tmp.csv")
# upcoming_matches_df = pd.read_csv("tmp.csv")
upcoming_matches_df.columns = map(str.lower, upcoming_matches_df.columns)
upcoming_matches_df["date"] = [pd.to_datetime(x).date() for x in upcoming_matches_df["date"]]
# existing_matches = read_odds(leagues=league, countries=country)
num_new_matches = len(upcoming_matches_df)
df = combine_upcoming_and_old(upcoming_matches_df, country, league, existing_matches)
print(df)
df = calculate_features(df)
print(df)
df = df[-num_new_matches:].reset_index(drop=True)
return df
def scrape_historical(country, league, historical_df):
historical_df = historical_df.sort_values(by='date')
historical_df = historical_df[historical_df['country'] == country]
historical_df = historical_df[historical_df['league'] == league]
latest_match_df = historical_df[-1:]
new_df = scrape_historical_league(country, league, stop_at=latest_match_df)
new_df.columns = map(str.lower, new_df.columns)
new_df["date"] = [pd.to_datetime(x).date() for x in new_df["date"]]
num_new_matches = len(new_df)
df = combine_upcoming_and_old(new_df, country, league, historical_df)
df = calculate_features(df)
return df[-num_new_matches:].reset_index(drop=True)
if __name__ == "__main__":
country, league = 'england', 'premier-league'
# Scrape new data
# df = scrape_historical_league(country, league)
# Update an already existing dataframe
# Update all existing dataframes
update_all_leagues()
# # Scrape upcoming matches and adding features
# odds = read_odds(country, league)
# upcoming = scrape_upcoming(country, league, odds)
# print(upcoming)