python双色球数据抓取及模拟生成高概率的号码

1、代码分两部分,第一部分是抓取网站上的双色球历史数据并存储到数据库,这部分代码来自第三方的。

2、通过历史记录分配每个号的权重,并随机生成前6个号码 的序列。

import requests
from bs4 import BeautifulSoup
import json
import sqlite3
import chardet
import xlwt

cnt = input("请输入你要获取的数量(30,50,100):")
cntINT = int(cnt)
url = 'http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount='+cnt

try:
    if(0
#!/usr/bin/python3 

from bs4 import BeautifulSoup
import json
import sqlite3
import chardet
import xlwt
from collections import Counter
import matplotlib.pyplot as plt
import random
import numpy as np

#打开数据库sqlite
conn = sqlite3.connect('Ssqdata100.sqlite')
cur = conn.cursor()
#查询数据库
cur.execute("select * from RE")


b1list = []
b2list = []
b3list = []
b4list = []
b5list = []
b6list = []

bluelist = []

ballall_list = []

res_red = cur.fetchall()
cur.execute("select * from BL")
res_bl = cur.fetchall()
all_redball = []

for row_red, row_blue in zip(res_red, res_bl):
    redsq = row_red[1].split(",")
    all_redball.append(np.array([int(x) for x in redsq]))
    bluelist.append(row_blue[1])
    b1list.append(redsq[0])
    b2list.append(redsq[1])
    b3list.append(redsq[2])
    b4list.append(redsq[3])
    b5list.append(redsq[4])
    b6list.append(redsq[5])


l1 = Counter(b1list)
l2 = Counter(b2list)
l3 = Counter(b3list)
l4 = Counter(b4list)
l5 = Counter(b5list)
l6 = Counter(b6list)

l7 = Counter(bluelist)

#暂时不考虑最后一个球
#list_all = [l1, l2, l3, l4, l5, l6, l7]
list_all = [l1, l2, l3, l4, l5, l6]
list_index = 1

#存储1-7号球的球号对应出现频率的dict对象
list_weight = []

for l1 in list_all:
    #排序后的标签
    s1_labels = list(sorted(l1.keys()))
    #每个标签对应的实际数值
    s1_fracs = [l1.get(s1_labels[i]) for i in range(len(s1_labels))]
    s1_sum = sum(s1_fracs)
    index = 0
    dict_weight = {}
    for key in s1_labels:
        freq = int(s1_fracs[index]/s1_sum*100)
        print("%d号球数字%s 出现概率:%d" %(list_index, key, freq))
        dict_weight[key] = freq
        index += 1
    
    list_weight.append(dict_weight)
    list_index += 1
    print("\r\n")


def ballgame(self):
    for data in list_weight:
        value_list = []
        getball = []
        for key, value in data.items():
            value_list += value*[key]
        pick_value = random.choice(value_list)
        #print(pick_value)
        getball.append(pick_value)
        #构造机器参数

        getball = np.array([int(x) for x in getball])

        for lst in all_redball:
            if sum(lst == getball) >= 5:
                print("more than five!!")
                print(lst, getball)


count = 0
while True:
    count += 1
    ballgame(None)

print(count)
print('finish')


'''

s1_labels = list(sorted(l1.keys()))
s1_fracs = [l1.get(s1_labels[i]) for i in range(len(s1_labels))]
fig = plt.figure()

plt.pie(x=s1_fracs, labels=s1_labels, autopct='%3.1f %%',shadow=True, labeldistance=1.1, startangle = 90,pctdistance = 0.6)

plt.legend(loc='upper left', bbox_to_anchor=(-0.1, 1))

plt.title('ball 1 数据分布图',fontsize=20)
plt.show()

'''


你可能感兴趣的:(linux,python,开发语言)