forked from msarnacki/flashscore-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
161 lines (123 loc) · 4.4 KB
/
test.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
import time
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re
import pandas as pd
import os
def driver_get_source_match_summary(url):
driver.get(url)
try:
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'summary-content')))
print('page loaded')
except TimeoutError:
print('loading took too much')
#time for loading all elements
#time.sleep(3.5)
page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
return soup
def driver_get_source_match_stats(url):
driver.get(url)
try:
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'statContent')))
print('page loaded')
except TimeoutError:
print('loading took too much')
#time for loading all elements
#time.sleep(3.5)
page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
return soup
def driver_get_source_match_lineups(url):
driver.get(url)
try:
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'lineups-wrapper')))
print('page loaded')
except TimeoutError:
print('loading took too much')
#time for loading all elements
#time.sleep(3.5)
page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
return soup
def driver_get_source_season(url):
driver.get(url)
#time for loading all elements
time.sleep(3.5)
try:
button_cookies = driver.find_element_by_xpath("//button[@id='onetrust-accept-btn-handler']")
button_cookies.click()
except:
print("cookies already closed")
#click "more matches" until it is possible
while('event__more' in driver.page_source):
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'event__more.event__more--static')))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
button_more = driver.find_element_by_class_name('event__more.event__more--static')
button_more.click()
#time.sleep(5)
page = driver.page_source
soup = BeautifulSoup(page, 'html.parser')
return soup
URL = 'https://www.flashscore.com/match/2JDks1o7/#match-summary'
URL2 = 'https://www.flashscore.com/match/2JDks1o7/#match-statistics;0'
URL3 = 'https://www.flashscore.com/match/2JDks1o7/#lineups;1'
#page = requests.get(URL).text
start = time.time()
driver = webdriver.Chrome()
#driver.get(URL)
#time.sleep(2)
#page = driver.page_source
#soup = BeautifulSoup(page, 'html.parser')
soup = driver_get_source_match_summary(URL)
league_round = soup.find('span', class_ = 'description__country').find('a').text
print(league_round)
odd = soup.find('tr', class_ = 'odd')
odds = odd.find_all(class_ = 'odds-wrap')
for odd in odds:
if '[' in odd['eu']:
odd1 = odd['eu'].split('[')[0]
odd2 = odd['eu'].split(']')[1]
else:
odd1 = odd['eu']
odd2 = odd.text
print(odd1)
print(odd2)
soup = driver_get_source_match_stats(URL2)
stats_home = soup.find_all(class_="statText statText--homeValue")
stats_away = soup.find_all(class_="statText statText--awayValue")
for i in range(len(stats_home)):
print(stats_home[i].text)
soup = driver_get_source_match_lineups(URL3)
lineups = soup.find('table', class_='parts')
print(lineups)
names_home = []
for tr in lineups.find_all('td', class_='summary-vertical fl'):
name_home = tr.find_all(class_='name')
#print(names_home)
for name in name_home:
if '(' in name.text:
#print(name.text[:-4])
names_home.append(name.text[:-4])
#match.append(name.text[:-4])
else:
#print(name.text)
names_home.append(name.text)
#match.append(name.text)
names_away = []
for tr in lineups.find_all('td', class_='summary-vertical fr'):
name_away = tr.find_all(class_='name')
for name in name_away:
if '(' in name.text:
#print(name.text[:-4])
names_away.append(name.text[:-4])
#match.append(name.text[:-4])
else:
#print(name.text)
names_away.append(name.text)
#match.append(name.text)
print(names_home)