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
| import requests import json from datetime import date import mysql.connector from sqlalchemy import create_engine import pandas as pd from sklearn.linear_model import LinearRegression
today = date.today()
cnx = mysql.connector.connect( host='', user='', password='', database='' )
engine = create_engine('mysql+mysqlconnector://', creator=lambda: cnx) cursor = cnx.cursor()
header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36' }
def days_between_dates(start_date, end_date): delta = end_date - start_date return delta.days
cursor.execute( 'SELECT lottery_date,issue,lottery_num FROM `array5` ORDER BY lottery_date DESC LIMIT 1') result = list(cursor.fetchall()) lotteryDateLast = result[0][0] issueLast = result[0][1]
days = days_between_dates(lotteryDateLast, today)
if days > 0: url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=350133&provinceId=0&pageSize={}&isVerify=1&pageNo=1'.format( days) response = requests.get(url, headers=header) content = response.content.decode('utf-8') js = json.loads(content) numbers = js.get('value') lists = numbers.get('list') for list in lists: issue = list.get('lotteryDrawNum') lotteryDate = list.get('lotteryDrawTime') lotteryNum = list.get('lotteryDrawResult') lotteryCount = list.get('prizeLevelList')[0].get('stakeCount') lotteryMoney = list.get('prizeLevelList')[0].get('stakeAmount') sales = list.get('totalSaleAmount') pond = list.get('poolBalanceAfterdraw') cursor.execute( 'SELECT count(1) AS count FROM `array5` WHERE issue = %s', [issue]) count = cursor.fetchone() if count[0] == 0: sql = "INSERT INTO `array5`(`issue`, `lottery_date`, `lottery_num`, `lottery_count`, `lottery_money`, `sales`, `pond`) VALUES (%s, %s, %s, %s, %s, %s, %s) " item = (issue, lotteryDate, lotteryNum, lotteryCount, lotteryMoney, sales, pond) cursor.execute(sql, item) cnx.commit()
def predict_num(num): query = "SELECT issue, lottery_date, SUBSTRING(lottery_num, {}, 1) AS lottery_num FROM array5 ORDER BY lottery_date DESC LIMIT 7".format( num) data = pd.read_sql(query, engine) X = data[['issue', 'lottery_date']] y = data['lottery_num']
X.loc[:, 'lottery_date'] = pd.to_datetime(X['lottery_date']).apply(lambda x: x.timestamp())
model = LinearRegression() model.fit(X, y)
new_data = pd.DataFrame( {'issue': [issueLast + 1], 'lottery_date': [today]}) new_data['lottery_date'] = pd.to_datetime( new_data['lottery_date']).apply(lambda x: x.timestamp()) new_X = new_data[['issue', 'lottery_date']] prediction = model.predict(new_X) rounded_prediction = int(round(prediction[0], 0)) return rounded_prediction
columns = [predict_num(1), predict_num(3), predict_num( 5), predict_num(7), predict_num(9)] randNums = ' '.join([str(column) for column in columns]) cursor.execute( 'SELECT count(1) AS count FROM `array5_rand` WHERE random_num = %s AND issue = %s', [randNums, issueLast + 1]) count = cursor.fetchone() if count[0] == 0: sql = "INSERT INTO `array5_rand`(`random_num`, `issue`) VALUES (%s, %s) " item = (randNums, issueLast + 1) cursor.execute(sql, item) cnx.commit() cursor.execute( 'SELECT count(1) AS count FROM `array5` WHERE lottery_num = %s', [randNums]) count = cursor.fetchone() print("开奖号码为:", randNums, "|| 此号码历史上共开出", count[0], "次")
cursor.close() cnx.close()
|