-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbet365_live.py
268 lines (246 loc) · 14.5 KB
/
bet365_live.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
# all leagues - live
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from datetime import datetime
import time
from loadMatch import loadMatch
import pandas as pd
# connect to the website
driver = webdriver.Chrome(executable_path=r'C:/Users/Angel/Downloads/chromedriver_win32/chromedriver_1.exe')
driver.get("https://www.bet365.com/#/IP/B18/")
main_window = driver.current_window_handle
print("Load all live games")
time.sleep(5)
''' switchButton '''
def loadPage(leagueName):
time.sleep(2)
now = str(datetime.now())
nowtime = now[:10] + '_' + now[11:13] + '-' + now[14:16] + '-' + now[17:19]
gameName = getName()
mylist = []
try:
loadMatch(leagueName, mylist, gameName, now)
time.sleep(1)
# to csv
df = pd.DataFrame(mylist, columns=['leagueName', 'gameName', 'type', 'Festival', 'fest', 'gameTime', 'Match', 'Head', 'Team', 'Odd_1', 'Odd_2', 'Odd_3', 'createTime'])
df.to_csv('result/Bet365Data/live/' + gameName + '_' + nowtime + '.csv', index=False)
except:
print("one error")
print(mylist[-1])
df = pd.DataFrame(mylist,columns=['leagueName', 'gameName', 'type', 'Festival', 'fest', 'gameTime', 'Match', 'Head','Team', 'Odd_1', 'Odd_2', 'Odd_3', 'createTime'])
df.to_csv('result/Bet365Data/live/' + gameName + '_' + nowtime + '.csv', index=False)
return
''' get Name '''
def getName():
gameName = driver.find_element_by_xpath('//div[@class="ipe-EventHeader_Fixture "]').get_attribute(
'innerHTML').lstrip()
gameName = gameName.replace('@', 'v')
extra = ['?', '/', '.', ',', '\\', '*', '<', '>', '\'', '\"', '|', '`', '~', '!']
for ex in extra:
gameName = gameName.replace(ex, '')
print(gameName)
return gameName
''' mainMatch '''
def mainMatch(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now):
path = '//div[@class="sip-MarketGroup "][' + str(block) + ']/div[2]/div[@class="gl-MarketGroupContainer "]'
length = len(driver.find_elements_by_xpath(path + '/div[1]/div'))
for i in range(2, length + 1):
Match = driver.find_element_by_xpath(path + '/div[1]/div[' + str(i) + ']/div').get_attribute('innerHTML').lstrip()
Team = ''
Odd_3 = ''
check = driver.find_elements_by_xpath(path + '/div[2]/div[' + str(i) + ']/span')
if len(check) > 1:
Head = driver.find_element_by_xpath(path + '/div[2]/div[' + str(i) + ']/span[1]').get_attribute('innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path + '/div[2]/div[' + str(i) + ']/span[2]').get_attribute('innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '/div[3]/div[' + str(i) + ']/span[2]').get_attribute('innerHTML').lstrip()
else:
Head = ''
Odd_1 = driver.find_element_by_xpath(path + '/div[2]/div[' + str(i) + ']/span[1]').get_attribute('innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '/div[3]/div[' + str(i) + ']/span[1]').get_attribute('innerHTML').lstrip()
mylist.append([leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, Team, Odd_1, Odd_2, Odd_3, now])
time.sleep(0.1)
def resultTotal(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now):
path = '//div[@class="sip-MarketGroup "][' + str(block) + ']/div[2]/div[@class="gl-MarketGroupContainer "]'
length = len(driver.find_elements_by_xpath(path + '/div[1]/div'))
for i in range(2, length + 1):
Team = driver.find_element_by_xpath(path + '/div[1]/div[' + str(i) + ']/div').get_attribute('innerHTML').lstrip()
Match = ''
Head = ''
Odd_3 = ''
check = driver.find_elements_by_xpath(path + '/div[2]/div[' + str(i) + ']/span')
if len(check) > 1:
Head = driver.find_element_by_xpath(path + '/div[2]/div[' + str(i) + ']/span[@class="srb-ParticipantCenteredStackedMarketRow_Handicap"]').get_attribute('innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path + '/div[2]/div[' + str(i) + ']/span[@class="srb-ParticipantCenteredStackedMarketRow_Odds"]').get_attribute('innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '/div[3]/div[' + str(i) + ']/span[@class="srb-ParticipantCenteredStackedMarketRow_Odds"]').get_attribute('innerHTML').lstrip()
mylist.append([leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, Team, Odd_1, Odd_2, Odd_3, now])
time.sleep(0.1)
def teamPoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now):
path = '//div[@class="sip-MarketGroup "][' + str(block) + ']/div[2]/div/div'
Match = '球隊總分'
Odd_3 = ''
homeName = driver.find_element_by_xpath(path + '[1]/div[1]').get_attribute('innerHTML').lstrip()
awayName = driver.find_element_by_xpath(path + '[2]/div[1]').get_attribute('innerHTML').lstrip()
length = len(driver.find_elements_by_xpath(path + '[1]/div'))
for l in range(2, length, 2):
Head = driver.find_element_by_xpath(path + '[1]/div[' + str(l) + ']/span[1]').get_attribute(
'innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path + '[1]/div[' + str(l) + ']/span[2]').get_attribute(
'innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '[1]/div[' + str(l + 1) + ']/span[2]').get_attribute(
'innerHTML').lstrip()
mylist.append([leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, homeName, Odd_1, Odd_2, Odd_3, now])
length = len(driver.find_elements_by_xpath(path + '[2]/div'))
for l in range(2, length, 2):
Head = driver.find_element_by_xpath(path + '/div[' + str(l) + ']/span[1]').get_attribute(
'innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path + '/div[' + str(l) + ']/span[2]').get_attribute(
'innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '/div[' + str(l + 1) + ']/span[2]').get_attribute(
'innerHTML').lstrip()
mylist.append([leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, awayName, Odd_1, Odd_2, Odd_3, now])
time.sleep(0.1)
def firstGet(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now):
path = '//div[@class="sip-MarketGroup "][' + str(block) + ']/div[2]/div/div'
length = len(driver.find_elements_by_xpath(path + '[1]/div'))
Match = ''
Team = ''
for l in range(2, length + 1):
Head = driver.find_element_by_xpath(path + '[1]/div[' + str(l) + ']/div').get_attribute('innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path + '[2]/div[' + str(l) + ']/span').get_attribute('innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '[3]/div[' + str(l) + ']/span').get_attribute('innerHTML').lstrip()
Odd_3 = driver.find_element_by_xpath(path + '[4]/div[' + str(l) + ']/span').get_attribute('innerHTML').lstrip()
mylist.append(
[leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, Team, Odd_1, Odd_2, Odd_3, now])
time.sleep(0.1)
def givePoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now):
path = '//div[@class="sip-MarketGroup "][' + str(block) + ']/div[2]/div/div'
length = len(driver.find_elements_by_xpath(path + '[1]/div'))
Match = '讓分'
Team = ''
Odd_3 = ''
for l in range(2, length+1):
Head = driver.find_element_by_xpath(path+'[1]/div[' + str(l) + ']/span[2]').get_attribute('innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path+'[1]/div[' + str(l) + ']/span[3]').get_attribute('innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path+'[1]/div[' + str(l) + ']/span[3]').get_attribute('innerHTML').lstrip()
mylist.append([leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, Team, Odd_1, Odd_2, Odd_3, now])
time.sleep(0.1)
def attachTotal(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now):
Match = '總分'
Team = ''
Odd_3 = ''
path = '//div[@class="sip-MarketGroup "][' + str(block) + ']/div[2]/div/div'
length = len(driver.find_elements_by_xpath(path + '[1]/div'))
for l in range(2, length + 1):
Head = driver.find_element_by_xpath(path + '[1]/div[' + str(l) + ']/div').get_attribute('innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path + '[2]/div[' + str(l) + ']/span').get_attribute('innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '[3]/div[' + str(l) + ']/span').get_attribute('innerHTML').lstrip()
mylist.append([leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, Team, Odd_1, Odd_2, Odd_3, now])
time.sleep(0.1)
''' totalPoints '''
def totalPoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now):
path = '//div[@class="sip-MarketGroup "][' + str(block) + ']/div[2]/div/div'
length = len(driver.find_elements_by_xpath(path+'[1]/div'))
Match = ''
Team = ''
Odd_3 = ''
for l in range(2, length+1):
Head = driver.find_element_by_xpath(path + '[1]/div[' + str(l) + ']/div').get_attribute('innerHTML').lstrip()
Odd_1 = driver.find_element_by_xpath(path + '[2]/div[' + str(l) + ']/span').get_attribute('innerHTML').lstrip()
Odd_2 = driver.find_element_by_xpath(path + '[3]/div[' + str(l) + ']/span').get_attribute('innerHTML').lstrip()
mylist.append([leagueName, gameName, 'Live', Festival, gameFest, gameTime, Match, Head, Team, Odd_1, Odd_2, Odd_3,now])
time.sleep(0.1)
''' load matches '''
def loadMatch(leagueName, mylist, gameName, now):
time.sleep(3)
blocksRoot = '//div[@class="sip-MarketGroup "]'
blocks = len(driver.find_elements_by_xpath(blocksRoot))
if blocks < 1:
return
# extend the list
print("展翅")
for block in range(1, blocks+1):
path = blocksRoot + '[' + str(block) + ']'
if len(driver.find_elements_by_xpath(path + '/div')) < 2:
button = driver.find_element_by_xpath(path + '/div[1]')
ActionChains(driver).move_to_element(button)
button.click()
time.sleep(0.1)
print("啰资料", blocks)
# load all blocks' data
for block in range(1, blocks+1):
try:
Festival = driver.find_element_by_xpath(blocksRoot + '[' + str(block) + ']/div[1]/div[@class="sip-MarketGroupButton_Text "]').get_attribute('innerHTML').lstrip()
print(Festival)
if len(driver.find_elements_by_xpath('//div[@class="ipe-EventHeader_Period "]')) > 0:
gameFest = driver.find_element_by_xpath('//div[@class="ipe-EventHeader_Period "]').get_attribute('innerHTML').lstrip()
else:
gameFest = ''
if len(driver.find_elements_by_xpath('//div[@class="ipe-EventHeader_ClockContainer "]')) > 0:
gameTime = driver.find_element_by_xpath('//div[@class="ipe-EventHeader_ClockContainer "]').get_attribute('innerHTML').lstrip()
else:
gameTime = ''
if Festival == "分數投注":
totalPoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif "投注" in Festival:
mainMatch(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif '奇/偶' in Festival:
mainMatch(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif '比賽結果和總分' in Festival:
resultTotal(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif "輸贏比數 3項" in Festival:
print(Festival, "not yet")
elif "節 輸贏比數" in Festival:
print(Festival, "not yet")
elif "輸贏比數" in Festival:
totalPoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif ("節" in Festival) & ("得分" in Festival):
totalPoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif ("附加" in Festival) & ("總分" in Festival):
totalPoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif "球隊總分" in Festival:
teamPoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif "附加讓分" in Festival:
givePoints(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif "附加比賽總分" in Festival:
attachTotal(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
elif "讓分 三項" in Festival:
print(Festival, "not yet")
elif "首先獲得" in Festival:
firstGet(leagueName, gameName, gameTime, gameFest, block, Festival, mylist, now)
else:
print(Festival, "extra")
except:
print(Festival, "loading error!")
time.sleep(0.1)
''' load betting list '''
def loadLeague():
try:
leaguePath = '//div[@class="ovm-Competition ovm-Competition-open "]'
leagues = driver.find_elements_by_xpath(leaguePath)
for league in range(1, len(leagues)+1):
leagueName = driver.find_element_by_xpath(leaguePath + '[' + str(league) + ']/div[1]/div/div[1]').get_attribute('innerHTML').lstrip()
# load games in one league
gamePath = leaguePath + '[' + str(league) + ']/div[2]/div'
games = driver.find_elements_by_xpath(gamePath)
print(leagueName, len(games))
for game in range(1, len(games)+1):
Go = driver.find_element_by_xpath(gamePath + '[' + str(game) + ']/div[1]/div[1]/div')
ActionChains(driver).move_to_element(Go)
Go.click()
loadPage(leagueName)
time.sleep(1)
driver.back()
time.sleep(1)
except:
loadLeague()
# start loading data
while True:
loadLeague()
time.sleep(5)
print('done')
time.sleep(1000)
driver.quit()