-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlt.py
161 lines (145 loc) · 5.45 KB
/
dlt.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
# coding: utf-8
"""
大乐透
Created on 2019-5-27 09:11:17
@author: modabao
"""
from datetime import datetime
import pandas as pd
from tools import config, get_last_term, get_html, add2db, get_history
config = config["dlt"]
class DLT(object):
"""
大乐透
"""
table_name = config['table_name']
field_names = config['field_names']
front_balls = config['front_balls']
back_balls = config['back_balls']
award_prize = config['award_prize']
all_balls = [['front'] * 35 + ['back'] * 12, front_balls + back_balls]
all_balls = pd.MultiIndex.from_tuples(list(zip(*all_balls)),
names=['part', 'ball'])
win_balls = [['front'] * 5 + ['back'] * 2, field_names[1:]]
win_balls = pd.MultiIndex.from_tuples(list(zip(*win_balls)),
names=['part', 'ball'])
url_template = (
'https://datachart.500.com/ssq/history/newinc/history.php?'
'start={start}&end={end}')
xpath_records = '//*[@id="tdata"]/tr'
xpath_record = './td[position()<=8]/text()'
def __init__(self):
""""""
self.history = None
self.update()
@classmethod
def update(cls):
"""
更新大乐透数据
:return:
"""
# 读取上一期已保存的期号
last_term = get_last_term(cls.table_name, cls.field_names)
start_term = f'{int(last_term)+1:05d}'
end_term = f'{datetime.now().year % 100}999'
url = cls.url_template.format(start=start_term, end=end_term)
html = get_html(url)
records = [x.xpath(cls.xpath_record)
for x in html.xpath(cls.xpath_records)]
if records:
add2db(cls.table_name, cls.field_names, records)
new_last_term = records[0][0]
else:
new_last_term = last_term
return last_term, new_last_term
def get_history(self, term=None, start_term='00000', end_term='99999', terms=None):
"""
获取大乐透历史开奖纪录
:param term:
:param start_term:
:param end_term:
:param terms:
:return:
"""
self.history = get_history(self.table_name, self.field_names, term,
start_term, end_term, terms)
self.history.columns = self.win_balls
def get_freq(self, history=None, terms=1, step=None):
"""
求一段时间内各号出现次数
"""
if not history and hasattr(self, 'history'):
history = self.history
elif not history:
self.get_history()
history = self.history
index = history.index[:terms] if terms else history.index
freq = []
if step:
for n, x in enumerate(index):
freq.append(self.get_freq_core(history.iloc[n:n+step]))
else:
for n, x in enumerate(index):
freq.append(self.get_freq_core(history.iloc[n:]))
freq = pd.DataFrame(freq, index=index, columns=self.all_balls)
return freq
def check(self, predict_df, reality_df=None):
"""
检查预测的获奖情况
:param predict_df: DataFrame,index设为term
:param reality_df: DataFrame,index设为term
:return: <pd.DataFrame>中奖等级
"""
if not reality_df and self.history:
reality_df = self.history
elif not reality_df:
start_term = predict_df.index.min()
end_term = predict_df.index.max()
self.get_history(start_term, end_term)
reality_df = self.history
index = [x for x in predict_df.index if x in reality_df.index]
if not index:
return None
predict_df = predict_df.loc[index]
reality_df = reality_df.loc[index]
records = []
for n in range(len(index)):
f = len(set(predict_df.iloc[n]['front']) &
set(reality_df.iloc[n]['front']))
b = len(set(predict_df.iloc[n]['back']) &
set(reality_df.iloc[n]['back']))
if f == 5 and b == 2:
level = '一等奖'
elif f == 5 and b == 1:
level = '二等奖'
elif f == 5 and b == 0:
level = '三等奖'
elif f == 4 and b == 2:
level = '四等奖'
elif f == 4 and b == 1:
level = '五等奖'
elif f == 3 and b == 2:
level = '六等奖'
elif f == 4 and b == 0:
level = '七等奖'
elif (f == 3 and b == 1) or (f == 2 and b == 2):
level = '八等奖'
elif ((f == 3 and b == 0) or (f == 1 and b == 2) or
(f == 2 and b == 1) or (f == 0 and b == 2)):
level = '九等奖'
else:
level = '血亏'
records.append([level, self.award_prize[level]])
records = pd.DataFrame(records, index=index,
columns=['award', 'prize'])
return records
def get_freq_core(self, history):
"""
"""
front_list = history['front'].values.flatten().tolist()
back_list = history['back'].values.flatten().tolist()
nums = len(history)
front_freq = [front_list.count(x) / nums for x in self.front_balls]
back_freq = [back_list.count(x) / nums for x in self.back_balls]
freq = front_freq + back_freq
return freq