【娱乐】通过python来预测下双色球


  自娱自乐,根据上一期中奖数字,取下一期经常出现的数字,(并不准,不可能中奖,纯粹自娱自乐)

在当前目录下新建一个excel,名字为123即可

excel格式:

 

正式代码如下:

  1 import requests
  2 import pandas as pd
  3 from bs4 import BeautifulSoup as BS
  4 from openpyxl import load_workbook
  5 
  6 path = './123.xlsx'
  7 wb = load_workbook(path)
  8 ws = wb['Sheet1']
  9 class TwoColorBall:
 10     def get_page(self,url):
 11         headers = {
 12             "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400",
 13             "Host":"kaijiang.zhcw.com"
 14         }
 15         res =requests.get(url =url,headers=headers)
 16         text = BS(res.text,"html.parser")
 17         return text
 18 
 19     def get_text(self):
 20         '''将结果插入excel'''
 21         ws.delete_rows(2,ws.max_row)
 22         for i in range(1,130):
 23             url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list_{}.html'.format(i)
 24             text = self.get_page(url)
 25             tr_table = text.find_all("tr")
 26             for tr_num in range(2,len(tr_table)-1):
 27                 td_table = tr_table[tr_num].find_all('td')
 28                 ems = td_table[2].find_all("em")
 29                 ws.cell(ws.max_row + 1, 1, value=td_table[0].get_text())
 30                 ws.cell(ws.max_row, 2,value=td_table[1].get_text())
 31                 ws.cell(ws.max_row, 3, value=ems[0].get_text())
 32                 ws.cell(ws.max_row, 4, value=ems[1].get_text())
 33                 ws.cell(ws.max_row, 5, value=ems[2].get_text())
 34                 ws.cell(ws.max_row, 6, value=ems[3].get_text())
 35                 ws.cell(ws.max_row, 7, value=ems[4].get_text())
 36                 ws.cell(ws.max_row, 8, value=ems[5].get_text())
 37                 ws.cell(ws.max_row, 9, value=ems[6].get_text())
 38         wb.save('./123.xlsx')
 39         wb.close()
 40 
 41     def get_count(self,column):
 42         df = pd.DataFrame(pd.read_excel(path))
 43         first = df.loc[:,column].value_counts()
 44         return first.index[0]
 45 
 46     def get_most_frequent_number(self):
 47         '''
 48         统计往期每个位置上出现过最多的球
 49         :return:
 50         '''
 51         most_frequent_number = []
 52         column    =['first','second','third','fourth','fifth','sixth','basketball']
 53         for i in column:
 54             num = self.get_count(i)
 55             most_frequent_number.append(num)
 56         return most_frequent_number
 57 
 58     def get_column_values(self,column):
 59         '''
 60         获取column列的所有值
 61         :param column:
 62         :return:
 63         '''
 64         rows = ws.max_row
 65         columndata = []
 66         for i in range(2, rows + 1):
 67             cellvalue = ws.cell(row=i, column=column).value
 68             columndata.append(cellvalue)
 69         columndata.reverse()
 70         return columndata
 71 
 72     def count_the_number_of_occurrences(self,column):
 73         '''
 74         统计红球每个数后面出现的次数
 75         :return:
 76         '''
 77         first = {}
 78         columndata = self.get_column_values(column)
 79         for num in range(1,len(columndata)):
 80             for f_num in range(1,33):
 81                 for a in range(1,33):
 82                     previous = columndata[num-1] # 当前位数
 83                     next_num =  columndata[num]  #后一位
 84 
 85                     if previous=='{}'.format('%02d'%f_num) and next_num=='{}'.format('%02d'%a):
 86                         if previous+next_num in first.keys():
 87                             first[previous+next_num] += 1
 88                         else:
 89                             first[previous+next_num]=1
 90         return first
 91 
 92     def count_the_number_of_occurrences_basketball(self,column):
 93         '''
 94         统计蓝球每个数后面出现的次数
 95         :return:
 96         '''
 97         first = {}
 98         columndata = self.get_column_values(column)
 99         for num in range(1,len(columndata)):
100             for f_num in range(1,17):
101                 for a in range(1,17):
102                     previous = columndata[num-1] # 当前位数
103                     next_num =  columndata[num]  #后一位
104                     if previous=='{}'.format('%02d'%f_num) and next_num=='{}'.format('%02d'%a):
105                         if previous+next_num in first.keys():
106                             first[previous+next_num] += 1
107                         else:
108                             first[previous+next_num]=1
109         return first
110 
111     def get_balls_with_the_greatest_chance(self,last_num,num_dict):
112         '''
113         根据上一期结果返回每个球后出现几率最大的一个球
114         :return:
115         '''
116         dict_num = {}
117         for i in range(1,33):
118             a = last_num+'%02d'%i
119             if a in num_dict.keys():
120                 dict_num[a]=num_dict[a]
121         max_num = max(zip(dict_num.values(), dict_num.keys()))
122         return max_num[1]
123 
124     def main(self):
125         '''计算所有结果'''
126         self.get_text()
127         b = self.get_most_frequent_number()
128         first_dict = self.count_the_number_of_occurrences(3)
129         second_dict = self.count_the_number_of_occurrences(4)
130         third_dict = self.count_the_number_of_occurrences(5)
131         fourth_dict = self.count_the_number_of_occurrences(6)
132         fifth_dict = self.count_the_number_of_occurrences(7)
133         sixth_dict = self.count_the_number_of_occurrences(8)
134         basketball_dict = self.count_the_number_of_occurrences_basketball(9)
135         custom_period =2
136         first_half =ws.cell(custom_period, 1).value
137         last_first = ws.cell(custom_period, 3).value
138         last_second = ws.cell(custom_period, 4).value
139         last_third = ws.cell(custom_period, 5).value
140         last_fourth = ws.cell(custom_period, 6).value
141         last_fifth = ws.cell(custom_period,7).value
142         last_sixth = ws.cell(custom_period, 8).value
143         last_basketball = ws.cell(custom_period, 9).value
144         next_first =self.get_balls_with_the_greatest_chance(last_first,first_dict)
145         next_second=self.get_balls_with_the_greatest_chance(last_second,second_dict)
146         next_third=self.get_balls_with_the_greatest_chance(last_third,third_dict)
147         next_fourth=self.get_balls_with_the_greatest_chance(last_fourth,fourth_dict)
148         next_fifth=self.get_balls_with_the_greatest_chance(last_fifth,fifth_dict)
149         next_sixth=self.get_balls_with_the_greatest_chance(last_sixth,sixth_dict)
150         next_basketball=self.get_balls_with_the_greatest_chance(last_basketball,basketball_dict)
151         print(f'根据上期{first_half}开奖结果红球1:{next_first[0:2]}后出现最多的是:{next_first[2:4]}')
152         print(f'根据上期{first_half}开奖结果红球2:{next_second[0:2]}后出现最多的是:{next_second[2:4]}')
153         print(f'根据上期{first_half}开奖结果红球3:{next_third[0:2]}后出现最多的是:{next_third[2:4]}')
154         print(f'根据上期{first_half}开奖结果红球4:{next_fourth[0:2]}后出现最多的是:{next_fourth[2:4]}')
155         print(f'根据上期{first_half}开奖结果红球5:{next_fifth[0:2]}后出现最多的是:{next_fifth[2:4]}')
156         print(f'根据上期{first_half}开奖结果红球6:{next_sixth[0:2]}后出现最多的是:{next_sixth[2:4]}')
157         print(f'根据上期{first_half}开奖结果蓝球:{next_basketball[0:2]}后出现最多的是:{next_basketball[2:4]}')
158         print('#'*60)
159         print(f'每个位置上出现最多的球是:红球:{b[0]},{b[1]},{b[2]},{b[3]},{b[4]},{b[5]}  蓝球:{b[6]}')
160         print('#' * 60)
161         print(f'##########上期{first_half}开奖结果是:红球:{last_first},{last_second},{last_third},{last_fourth},'
162               f'{last_fifth},{last_sixth}  蓝球:{last_basketball}')
163         print('#' * 60)
164         print(f'根据上期结果预测下期的球:红球:{next_first[2:4]},{next_second[2:4]},{next_third[2:4]},{next_fourth[2:4]},'
165               f'{next_fifth[2:4]},{next_sixth[2:4]}  蓝球:{next_basketball[2:4]}')
166 
167 
168 if __name__ == '__main__':
169     a = TwoColorBall()
170     a.main()

 

 运行结果如下:

 

 

再次提醒!自娱自乐!没法保证中奖!

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM