-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_each_match_data.py
316 lines (267 loc) · 11.9 KB
/
get_each_match_data.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
# -*- coding: utf-8 -*-
import db_connecter
import logging
import re
import requests
from lxml import etree
from lxml import html
from logging import config
from random import randint
from time import sleep
class GetHtmlFailed(Exception):
pass
class GetEachMatchOdds:
defaultEncoding = 'utf-8'
def __init__(self, loglevel):
logging.config.fileConfig('logger.conf')
self.logger = logging.getLogger(loglevel)
self.db_conn = db_connecter.DBConnecter('db.conf', 'default')
self.db_conn.connect()
pass
def get_each_match_odds(self, group_size, start_offset, end_offset):
current_offset = start_offset
headers = {'User-Agent':'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', 'Referer':'https://developer.mozilla.org/en-US/docs/Web/JavaScript'}
euro_sql_str='''insert into euro_odds
(match_id,
result,
company,
priodds3,
priodds1,
priodds0,
nowodds3,
nowodds1,
nowodds0,
prichance3,
prichance1,
prichance0,
nowchance3,
nowchance1,
nowchance0,
priyrr,
nowyrr,
prikelly3,
prikelly1,
prikelly0,
nowkelly3,
nowkelly1,
nowkelly0
)
values
(
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s
)
'''
asia_sql_str='''insert into asia_odds
(match_id,
result,
company,
priodds3,
priconcede,
priodds0,
nowodds3,
nowconcede,
nowodds0
)
values
(
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s
)
'''
while(current_offset <= end_offset):
sql_str = 'select id, home_team_score, away_team_score, asia_data_url, euro_data_url from matchs limit %s offset %s'
self.db_conn.select_record(sql_str, (group_size, current_offset))
rows = self.db_conn.fetchall()
for row in rows:
while True:
try:
self.logger.info(str([row[0], row[1], row[2], row[3], row[4]]))
match_id = row[0]
if row[1] > row[2]:
result = 3
elif row[1] == row[2]:
result = 1
else:
result = 0
if re.search(r'^http:', row[3]):
asia_data_url = row[3]
else:
asia_data_url = 'http:' + row[3]
if re.search(r'^http:', row[4]):
euro_data_url = row[4]
else:
euro_data_url = 'http:' + row[4]
euro_r = requests.get(euro_data_url, headers=headers, timeout=5)
euro_r.encoding = euro_r.apparent_encoding
sleep(0.5)
asia_r = requests.get(asia_data_url, headers=headers, timeout=5)
asia_r.encoding = asia_r.apparent_encoding
_euro_html = html.fromstring(euro_r.text)
if (len(_euro_html.xpath('//table[@class="pl_table_data"]')) > 0):
all_euro_odds = self.parse_euro_new_html(euro_r)
all_asia_odds = self.parse_asia_new_html(asia_r)
for e_odds in all_euro_odds:
self.db_conn.insert_record(euro_sql_str, tuple([match_id, result] + e_odds))
for a_odds in all_asia_odds:
self.db_conn.insert_record(asia_sql_str, tuple([match_id, result] + a_odds))
else:
all_euro_odds = self.parse_euro_old_html(euro_r)
all_asia_odds = self.parse_asia_old_html(asia_r)
for e_odds in all_euro_odds:
self.db_conn.insert_record(euro_sql_str, tuple([match_id, result] + e_odds))
for a_odds in all_asia_odds:
self.db_conn.insert_record(asia_sql_str, tuple([match_id, result] + a_odds))
self.db_conn.commit()
#sleep(0.5)
except GetHtmlFailed as err:
self.logger.error("Get Html Failed: {0}. 2 seconds later retry to get....".format(err))
sleep(2)
continue
except Exception as err:
self.logger.error("Get Html Failed: {0}. 5 seconds later retry to get....".format(err))
sleep(5)
continue
break
current_offset += group_size
sleep(randint(1, 2))
self.db_conn.close()
def parse_euro_new_html(self, response):
_html = html.fromstring(response.text)
company_names = _html.xpath('//span[@class="quancheng"]/text()')
tables = _html.xpath('//table[@class="pl_table_data"]')
if(len(_html.xpath('//table[@class="pl_table_data"]')) == 0):
raise GetHtmlFailed("Can't get Euro odds")
if(len(company_names) < 1):
return []
odds_arr = []
for tb in tables[3::4]:
trs = tb.xpath('./tbody/tr')
pri_odds = trs[0].xpath('./td/text()')
last_odds = trs[1].xpath('./td/text()')
odds_arr.append([pri_odds, last_odds])
chance_arr = []
for tb in tables[4::4]:
trs = tb.xpath('./tbody/tr')
pri_chance = trs[0].xpath('./td/text()')
last_chance = trs[1].xpath('./td/text()')
chance_arr.append([pri_chance, last_chance])
yrr_arr = []
for tb in tables[5::4]:
trs = tb.xpath('./tbody/tr')
pri_yrr = trs[0].xpath('./td/text()')
last_yrr = trs[1].xpath('./td/text()')
yrr_arr.append([pri_yrr, last_yrr])
kelly_arr = []
for tb in tables[6::4]:
trs = tb.xpath('./tbody/tr')
pri_kelly = trs[0].xpath('./td/text()')
last_kelly = trs[1].xpath('./td/text()')
kelly_arr.append([pri_kelly, last_kelly])
all_company_odds = []
for c, odds, chance, yrr, kelly in zip(company_names, odds_arr, chance_arr, yrr_arr, kelly_arr):
all_company_odds.append([c.encode(GetEachMatchOdds.defaultEncoding),odds[0][0], odds[0][1], odds[0][2], \
odds[1][0], odds[1][1], odds[1][2], chance[0][0], chance[0][1], chance[0][2], chance[1][0], chance[1][1],\
chance[1][2], yrr[0][0], yrr[1][0], kelly[0][0], kelly[0][1], kelly[0][2], kelly[1][0], kelly[1][1], kelly[1][2]])
return all_company_odds
def parse_euro_old_html(self, response):
_html = html.fromstring(response.text)
trs = _html.xpath('//table[@id="datatb"]/tr')
all_company_odds = []
if(len(_html.xpath('//table[@id="datatb"]')) == 0):
raise GetHtmlFailed("Can't get Euro odds")
if(len(trs) < 4):
return []
for elem, next_elem in zip(trs[2::2], trs[3::2]+[trs[2]]):
elem_tds = elem.xpath('./td')
next_elem_tds = next_elem.xpath('./td')
if len(elem_tds) > 12:
if(len(elem_tds[1].xpath('./a/text()')) > 0):
company = elem_tds[1].xpath('./a/text()')[0].encode(GetEachMatchOdds.defaultEncoding)
else:
company = elem_tds[1].text.encode(GetEachMatchOdds.defaultEncoding)
all_company_odds.append([company, elem_tds[2].text, elem_tds[3].text, elem_tds[4].text, next_elem_tds[0].text, \
next_elem_tds[1].text, next_elem_tds[2].text, elem_tds[5].text, elem_tds[6].text, elem_tds[7].text, next_elem_tds[3].text, \
next_elem_tds[4].text, next_elem_tds[5].text, elem_tds[8].text, next_elem_tds[6].text, elem_tds[9].xpath('./span/text()')[0], \
elem_tds[10].xpath('./span/text()')[0], elem_tds[11].text, next_elem_tds[7].xpath('./span/text()')[0], \
next_elem_tds[8].xpath('./span/text()')[0], next_elem_tds[9].text])
return all_company_odds
def parse_asia_new_html(self, response):
_html = html.fromstring(response.text)
company_names = _html.xpath('//span[@class="quancheng"]/text()')
if(len(_html.xpath('//table[@class="pl_table_data"]')) == 0):
raise GetHtmlFailed("Can't get Asia odds")
if(len(company_names) < 1):
return []
tables = _html.xpath('//table[@class="pl_table_data"]')
now_odds_arr = []
pri_odds_arr = []
for tb1, tb2 in zip(tables[2::2], tables[3::2]):
tb1_tds = tb1.xpath('./tbody/tr/td/text()')
tb2_tds = tb2.xpath('./tbody/tr/td/text()')
now_odds_arr.append(tb1_tds[0:3])
pri_odds_arr.append(tb2_tds[0:3])
all_company_odds = []
for c, pri_odds, now_odds in zip(company_names, pri_odds_arr, now_odds_arr):
if(len(pri_odds) < 3) or (len(now_odds) < 3):
continue
all_company_odds.append([c.encode(GetEachMatchOdds.defaultEncoding), pri_odds[0].encode(GetEachMatchOdds.defaultEncoding), \
pri_odds[1].replace(u' \xa0', '').encode(GetEachMatchOdds.defaultEncoding), pri_odds[2].encode(GetEachMatchOdds.defaultEncoding), \
now_odds[0].replace(u'\u2193', '').replace(u'\u2191', '').encode(GetEachMatchOdds.defaultEncoding), \
now_odds[1].replace(u' \xa0', '').encode(GetEachMatchOdds.defaultEncoding), \
now_odds[2].replace(u'\u2193', '').replace(u'\u2191', '').encode(GetEachMatchOdds.defaultEncoding)])
return all_company_odds
def parse_asia_old_html(self, response):
_html = html.fromstring(response.text)
trs = _html.xpath('//table[@id="datatb"]//tr')
if(len(_html.xpath('//table[@id="datatb"]')) == 0):
raise GetHtmlFailed("Can't get Asia odds")
all_company_odds = []
number_re = re.compile(r'\d\.\d')
for tr in trs[1:-3]:
tds = tr.xpath('./td')
if(len(tds) > 8):
pri_home_odds = tds[6].text
pri_away_odds = tds[8].text
if len(tds[2].xpath('./span/text()')) < 1 or len(tds[4].xpath('./span/text()')) < 1:
continue
new_home_odds = tds[2].xpath('./span/text()')[0]
new_away_odds = tds[4].xpath('./span/text()')[0]
if not number_re.search(pri_home_odds) or not number_re.search(pri_away_odds) or not number_re.search(new_home_odds) or \
not number_re.search(new_away_odds) or not tds[1].text or not tds[3].text or not tds[7].text:
continue
all_company_odds.append([tds[1].text.replace(u'\ufffd', '').encode(GetEachMatchOdds.defaultEncoding), \
pri_home_odds, tds[7].text.replace(u' \xa0', '').encode(GetEachMatchOdds.defaultEncoding), \
pri_away_odds, new_home_odds, tds[3].text.replace(u' \xa0', '').encode(GetEachMatchOdds.defaultEncoding), new_away_odds])
return all_company_odds
if __name__ == '__main__':
g = GetEachMatchOdds('product')
g.get_each_match_odds(200, 40562, 50000)