# coding=utf-8
from pyquery import PyQuery as pq
import requests
import pymysql
import time
import Config
host = 'localhost'
user = 'root'
# pwd = 'eagle666'
pwd = 'lottery@2017'
db_name = 'db_4dd'
num_all = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
############################################################################
################### 获取2004-当天的所有记录 #############################
############################################################################
# 写入数据
def replaceDB(sqls):
db = pymysql.connect(host, user, pwd, db_name, charset='utf8')
cursor = db.cursor()
try:
for i in sqls:
cursor.execute(i)
db.commit()
except Exception as e:
db.rollback()
print(e)
# 获取所有历史记录
def getHtmlResult(year):
url = 'https://kaijiang.78500.cn/p5/'
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
}
data = {
'startqi': '2019001',
'endqi': '2019351',
'year': year,
'action': 'years'
}
content = requests.post(url=url, headers=headers, data=data)
if content.status_code == 200:
html = content.text
trList = pq(html)('.kjls tbody tr')
sqls = []
for tr in trList:
tdList = pq(tr)('td')
period = tdList[0].text
if period is not None:
date = tdList[1].text
result = pq(tdList[3])('a').text().split(' ')
# print(period)
# print(date)
# print(result)
sql = "REPLACE INTO t_history (id, period, a, b, c, d, e, createTime) VALUES (UUID(), '%s', '%s', '%s', '%s', '%s', '%s', '%s');" % \
(period, result[0], result[1], result[2], result[3], result[4], date)
sqls.append(sql)
print(sqls)
replaceDB(sqls)
def startAll():
# 获取从2004-当前的所有数据
for i in range(0, 17):
y = (2004 + i)
getHtmlResult(str(y))
############################################################################
##################### 获取当天记录 ######################################
############################################################################
def startToday():
url = 'https://kaijiang.78500.cn/p5/'
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
}
content = requests.get(url=url, headers=headers)
if content.status_code == 200:
html = content.text
trList = pq(html)('.kjls tbody tr')
sqls = []
date = time.strftime('%Y-%m-%d', time.localtime())
for tr in trList:
tdList = pq(tr)('td')
period = tdList[0].text
if period is not None and date == tdList[1].text:
result = pq(tdList[3])('a').text().split(' ')
sql = "REPLACE INTO t_history (id, period, a, b, c, d, e, createTime) VALUES (UUID(), '%s', '%s', '%s', '%s', '%s', '%s', '%s');" % \
(period, result[0], result[1], result[2], result[3], result[4], date)
sqls.append(sql)
break
if len(sqls) > 0:
replaceDB(sqls)
print(date + ' today success!')
############################################################################
################### 生成2020全部结果的概率表数据 #########################
############################################################################
# 查询数据
def queryDB(sql):
db = pymysql.connect(host, user, pwd, db_name, charset='utf8')
cursor = db.cursor()
try:
cursor.execute(sql)
return cursor.fetchall()
except Exception as e:
print(e)
return ''
# 根据公式计算末位余数
def calc(a, b, c, d, e, formula):
# print(formula)
new = formula.replace('A', a) \
.replace('B', b) \
.replace('C', c) \
.replace('D', d) \
.replace('E', e) \
# print(new)
# print(eval(new))
return str(eval(new))[-1]
# []转带,的字符串
def formatCode(list):
code = ''
for i in list:
code += (i + ',')
return code[:-1]
def generateDayResult(day):
# 根据日期查出前一天的开奖结果
sql = 'SELECT a, b, c, d, e FROM t_history WHERE createTime < \''+day+'\' ORDER BY createTime DESC LIMIT 1;'
preData = (queryDB(sql)[0])
print(preData)
########################### 计算排除A ##########################
aList = []
# 1、上一期第一位
# aList.append((preData[0]))
s1 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.A11)
# if s1 not in aList:
aList.append(s1)
s2 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.A12)
# if s2 not in aList:
aList.append(s2)
s3 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.A13)
# if s3 not in aList:
aList.append(s3)
s4 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.A14)
# if s4 not in aList:
aList.append(s4)
s5 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.A15)
# if s5 not in aList:
aList.append(s5)
print(aList)
########################### 计算剩余A ###########################
aList2 = []
for i in num_all:
if i not in aList:
aList2.append(i)
print(aList2)
########################### 计算排除B ##########################
bList = []
# 1、上一期第一位
# bList.append((preData[1]))
b1 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.B11)
# if b1 not in bList:
bList.append(b1)
b2 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.B12)
# if b2 not in bList:
bList.append(b2)
b3 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.B13)
# if b3 not in bList:
bList.append(b3)
b4 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.B14)
# if b4 not in bList:
bList.append(b4)
b5 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.B15)
# if b4 not in bList:
bList.append(b5)
print(bList)
########################### 计算剩余B ###########################
bList2 = []
for i in num_all:
if i not in bList:
bList2.append(i)
print(bList2)
########################### 计算排除C ##########################
cList = []
# 1、上一期第一位
# cList.append((preData[2]))
c1 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.C11)
# if c1 not in cList:
cList.append(c1)
c2 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.C12)
# if c2 not in cList:
cList.append(c2)
c3 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.C13)
# if c3 not in cList:
cList.append(c3)
c4 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.C14)
# if c4 not in cList:
cList.append(c4)
c5 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.C15)
# if c4 not in cList:
cList.append(c5)
print(cList)
########################### 计算剩余C ###########################
cList2 = []
for i in num_all:
if i not in cList:
cList2.append(i)
print(cList2)
########################### 计算排除D ##########################
dList = []
# 1、上一期第一位
# dList.append((preData[3]))
d1 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.D11)
# if d1 not in dList:
dList.append(d1)
d2 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.D12)
# if d2 not in dList:
dList.append(d2)
d3 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.D13)
# if d3 not in dList:
dList.append(d3)
d4 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.D14)
# if d4 not in dList:
dList.append(d4)
d5 = calc(preData[0], preData[1], preData[2], preData[3], preData[4], Config.D15)
# if d4 not in dList:
dList.append(d5)
print(dList)
########################### 计算剩余D ###########################
dList2 = []
for i in num_all:
if i not in dList:
dList2.append(i)
print(dList2)
insertSQL = "REPLACE INTO `db_4dd`.`t_compare_m2`(`id`, `day`, `delA`, `delB`, `delC`, `delD`, `surA`, `surB`, `surC`, `surD`) VALUES (uuid(), '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');" % \
(day, formatCode(aList), formatCode(bList), formatCode(cList), formatCode(dList), formatCode(aList2), formatCode(bList2), formatCode(cList2), formatCode(dList2))
return insertSQL
def generateAll():
# 获取2020所有结果
sql = '''
SELECT createTime FROM t_history WHERE createTime like '2020%' ORDER BY createTime DESC;
'''
timeList = queryDB(sql)
updateSQLs = []
for i in timeList:
updateSQLs.append(generateDayResult(i[0]))
replaceDB(updateSQLs)
############################################################################
################### 生成当天的概率表数据 #########################
############################################################################
def generate():
# 生成当天未出结果前的概率值
today = time.strftime('%Y-%m-%d', time.localtime())
hour = time.strftime('%H', time.localtime())
# 每天20:40出结果,20点之前可以生成当天概率数据,不会包含今天的结果
# 定时任务放在:2:00
if int(hour) < 20:
updateSQLs = []
updateSQLs.append(generateDayResult(today))
replaceDB(updateSQLs)
print(today + ' generate success!')
# generateAll()
generate()
# startAll()
startToday()