python + ロト６

# 概要
サイコロを６万回を回すとそれぞれ1万に近くなる。
これをロト６に適用してロト６をあてる確率を少し上げてみよう。

# 構造
１．mizuho銀行のWEBからロト６DBを引っ張る。（requests利用）
２．DBはNUMPYを利用しローカルで管理する
３．ロト番号１～４３までの数字がでた頻度を計算する。
４．頻度数1番目、2番目は取り除く（choose(flist,ign,topn,topc)で調節可能）
５．頻度数上位17まで数字をランダムで2個、以降からは4個を抽出する。
６．履歴はローカルでpast.txtで管理




```
from bs4 import BeautifulSoup
import requests
from urllib.request import urlopen
import re
import pandas as pd
import numpy as np

def oldurl():
    ulist=[]
    for a in range(60001,60442,20):
        url='https://www.mizuhobank.co.jp/retail/takarakuji/loto/backnumber/loto' + str(a) +'.html'
        ulist.append(url)
    return ulist

def setdb_first():

    ulist=oldurl()
    lolist=[]
    for url in ulist:
        r = requests.get(url)
        r.encoding = r.apparent_encoding
        soup = BeautifulSoup(r.text, "html.parser")

        table=soup.findAll("table", {"class":"typeTK"})
        if len(table):
            table =table[0]
            table=str(table)
            pdtable=pd.read_html(table) #to pandas
            
            for i in pdtable[0].itertuples():
                i=list(i)
                i=i[1:]
                i[0]=int(re.findall('\d+', i[0])[0]) #get
                datelist=re.findall('\d+', i[1])
                datelist[1]=str(datelist[1]).zfill(2)
                datelist[2]=str(datelist[2]).zfill(2)
                i[1]=int(''.join(datelist))
                lolist.append(i)
                #print(i) 
        else:
            print('set db: failed ')
            print('no data:')
            return False
    
    lonp=np.array(lolist)
    np.save('oldlo', lonp)

def setdb_second(start):
    newlo=[]
    cnt=0
    while True:
        cnt+=1
        times=str(start+cnt).zfill(4)
        r = requests.get('https://www.mizuhobank.co.jp/retail/takarakuji/loto/loto6/csv/A102'+times+'.CSV')
        r.encoding = r.apparent_encoding
        soup = BeautifulSoup(r.text, "html.parser")
        regex=re.compile("(\d+)[,](\d+)[,](\d+)[,](\d+)[,](\d+)[,](\d+)[,]\w+[,](\d+)")
        lolist=[(start+cnt)]
        soup=str(soup)
        result=regex.search(soup)
        if result:
            for a in range(1,8,1):
                lolist.append(result.group(a))
            newlo.append(lolist)
        else:
            break
        if (cnt%10)==0:
            print('complete: ',start+cnt)

    lonp=np.array(newlo)
    np.save('newlo', lonp)
    print(lonp)    
    
def update():
    before=np.load('newlo.npy',allow_pickle=True)
    
    start=before.shape[0]+460 #460>old times
    newlo=[]
    cnt=0
    while True:
        cnt+=1
        times=str(start+cnt).zfill(4)
        
        r = requests.get('https://www.mizuhobank.co.jp/retail/takarakuji/loto/loto6/csv/A102'+times+'.CSV')
        r.encoding = r.apparent_encoding
        soup = BeautifulSoup(r.text, "html.parser")
        regex=re.compile("(\d+)[,](\d+)[,](\d+)[,](\d+)[,](\d+)[,](\d+)[,]\w+[,](\d+)")
        lolist=[(start+cnt)]
        soup=str(soup)
        result=regex.search(soup)
        if result:
            print("updated! :",times,"th")
            for a in range(1,8,1):
                lolist.append(result.group(a))
            newlo.append(lolist)
        else:
            break
        if (cnt%10)==0:
            print('complete: ',start+cnt)
    if cnt==1:
        print("Nothing to update")
    else:
        lonp=np.array(newlo)
        sumlo=np.concatenate((before,lonp), axis=0)
        np.save('newlo', sumlo)
  
    
    
def freq(loset,times): #frequently to times
    
    lo=loset[:times,1:7]
    unique, counts=np.unique(lo,return_counts=True)
    counts=np.asarray(counts)
    unique=np.asarray(unique)
    unique.resize(1,counts.shape[0])
    i=np.argsort(counts)[::-1]
    c=unique[:,i]
    print("total of lotto6: ",times)
    
    tmp=0
    frq=c[0,:]
    nums=np.sort(counts)[::-1]
    for a in range(43):
        check='no.'+str(a+1)+ ' > '+str(frq[a])+':'+str(nums[a])+' times'
        print(check)

    
    return c[0,:]
    
def freqs(li):
    li=np.array(li)
    unique, counts=np.unique(li,return_counts=True)
    counts=np.asarray(counts)
    unique=np.asarray(unique)
    unique.resize(1,counts.shape[0])
    i=np.argsort(counts)[::-1]
    c=unique[:,i]  
    
    
    
def scen(loset,times,stand): #frequently from times,0-42>1-43
    checkset=loset[times:,:]#1000>1001-
    cnt=0
    yn=[0,0,0]
    scontlist=[]
    mcountlist=[]
    for a in checkset:
        sdata=freq(loset,times+cnt)
        cont=0
        min=42
        for b in a[1:7]:
            #print(b)
            loc = np.where(sdata == b)
            if loc[1][0]<min:
                min=loc[1][0]
            if loc[1][0]>stand:    
                cont+=1
        scontlist.append(cont)
        mcountlist.append(min)
        if min>stand:
            yn[0]+=1
        else:
            yn[1]+=1
        cnt+=1 
    freqs(mcountlist)
    return yn

def select(li,pick):
    selnum=[]
    cnt=0
    while True:
        ran=np.random.randint(0,li.size,1)
  
        try:
            selnum.index(ran[0])
        except:
            selnum.append(ran[0])
            cnt+=1
        if cnt==pick:
            break     
    cnt=0
    for a in selnum:
        selnum[cnt]=li[selnum[cnt]]
        cnt+=1
    return selnum

def choose(flist,ign,topn,topc):
    flist=flist[ign:]
    result=select(flist[:topn],topc)
    print("list: ",flist[:topn].shape[0],flist[:topn],result)
    return result

def dupli(li,pick):

    li=li[:,1:7]
    #print(li[3])
    i=np.argsort(pick)
    pick=pick[i]
    #pick=np.array([16,18,26,27,34,40])
    result=np.where((li == pick).all(axis=1))
        
    if result[0].size!=0:
        print("Wow! it was", result[0],"th")
        print("Try again!!")
        return False
    
    return True
        
def checksave(sumlo,li):
    regex=re.compile("(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)")
    futurehope="\n"+str(sumlo[-1,0]+1) + str(li)
    
    with open('past.txt', 'r+') as f:
        d = f.readlines()
        f.seek(0)
        for i in d:
            #print(i)
            if len(i)<2:
                print('')
            elif (i.find('hit') == -1):
                result=regex.search(i)
                cnt=0
                hope=[]
                besix=[]
                for a in range(1,8,1):
                    hope.append(int(result.group(a)))
                #print("test:",hope)
                ## completely check
                if sumlo[-1,0]<hope[0]:
                    f.write(i)
                    break
                
                if sumlo[hope[0]-1,0]!=hope[0]:
                    print("not times matech")
                    print(tt)
                ##
                for a in range(6):
                    hit=np.where(sumlo[hope[0]-1,1:]==hope[a])
                    if hit[0].size!=0:
                        cnt+=1
                        besix.append(hope[a])
                output=str(hope[0])+'/ '+ str(cnt) +'hit /'+str(hope[1:7])+'/'+str(sumlo[hope[0]-1,1:7])+'/'+str(besix)
                #print(output)
                print("Bless you: ",output)
                f.write('%s\n' %output)
                #f.write('%s\n' %output)
            else:
                f.write(i)
        f.write(futurehope)
    
    
    #if (string.find(CentoOS') != -1):
    
#setdb_first()
#setdb_second(460)
try:
    oldlo=np.load('oldlo.npy',allow_pickle=True)
except:
    setdb_first()
    setdb_second(460)
    oldlo=np.load('oldlo.npy',allow_pickle=True)
oldlo=np.delete(oldlo,1,1)

update()
newlo=np.load('newlo.npy',allow_pickle=True)

sumlo=np.concatenate((oldlo,newlo), axis=0)
sumlo=sumlo.astype('int16')
total=sumlo.shape[0]
#print("Total: ", total)
pri=freq(sumlo,total)# 1~3

#######for simulation
start_times=1000
#print(sumlo.shape)
#print(sumlo[0:4])

#######for simulation


ruckynum=5 #0 to 10

first=choose(pri,2,10+ruckynum,2) # no.3~17, in15 pick 2
second=choose(pri,12+ruckynum,28,4) # no.18~43, in26 pick 4

pick=np.hstack([first,second])

i=np.argsort(pick)
pick=pick[i]


if dupli(sumlo,pick):
    checksave(sumlo,pick)

print('good luck: ',pick)

```