社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python爬取双色球,妈妈再也不会担心我不会中奖了

〆 小源。 • 6 年前 • 615 次点击  
阅读 29

Python爬取双色球,妈妈再也不会担心我不会中奖了

一、概况

一般大家都能会有个财富自由、时间自由的梦想。除了我们勤奋努力外,有些人运气比较好,买了一注彩票,瞬间会走上人生巅峰。彩票确实让有些人实现了这个梦想,但是这个概率太低了,低到无法想象。所以我们还是努力奋斗,做一个追梦人吧!

我们今天就爬取有史以来所有的双色球数据,看看这些年哪些数字出现的次数比较高。有的小伙伴可能不知道双色球是什么?双色球顾名思义就是两种颜色的球,一个红色,一个蓝色。红球从1-33中取出6个,篮球从1-16取出1个,如果你买的跟开奖号码一样,恭喜你几百万到手。

二、分析网站

我们今天准备爬取的网站叫500彩票www.500.com/) 这个网站有很多彩票的开奖信息,我们找到全国开奖这个导航(kaijiang.500.com/)

开奖信息
在这我们可以查询任何的彩票开奖信息:

在这里插入图片描述
我们选择双色球,并打开浏览器调试:

在这里插入图片描述
可以看出来是一个ajax(kaijiang.500.com/static/info… 请求,并且返回很多的xml信息。信息里面包括了有史以来所有双色球的开奖号码信息。数据有了,我们完全可以用正则表达式把我们想要的数据提取出来,然后保存到数据库。

三、逻辑实现

我们在本地先创建好数据库,我们要的信息只有三个:

  • 红球
  • 篮球
  • 日期 代码比较简单:
import requests
import re
import pymysql

# 请求地址
url = 'http://kaijiang.500.com/static/info/kaijiang/xml/ssq/list.xml?_A=BLWXUIYA1546584359929'

# 数据库连接
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='123456',
                             db='db_shuangseqiu', )
# 获取游标对象
cursor = connection.cursor()

# 请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
# 发起请求
reponse = requests.get(url=url, headers=headers)

# 正则规则
pattern = re.compile(r'<row.*?opencode="(.*?)".*?opentime="(.*?)"')

# 双色球数据
ssq_data = pattern.findall(reponse.text)

# ('10,11,12,13,26,28|11', '2003-02-23 00:00:00')
for data in ssq_data:#处理数据
    info, date = data
    red, blue = info.split("|")

    #插入数据
    sql = """

       INSERT INTO ssq_info(red,blue,date)values (%s,%s,%s) 

       """
    try:
        cursor.execute(sql, [red, blue, date])
        connection.commit()
    except Exception as e:
        connection.rollback()

复制代码

从2003年第一个双色球开始,我们本地一共有2389条开奖数据。

在这里插入图片描述

四、统计

我们来统计一下这些年红球1-33出现的次数和篮球1-16出现的次数,通过matplotlib绘制成柱形统计图。在绘制前,我们得先从数据库中把红球和篮球出现的次数统计出来。

import pymysql
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
# 数据库连接
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='123456',
                       db='db_shuangseqiu', )
# 获取游标对象
cursor = connection.cursor()

reds_count = [] #1-33红球所有个数
blues_count = [] #1-16篮球所有个数

def getdata():
    sql = 'select * from ssq_info'
    cursor.execute(sql)
    results = cursor.fetchall()#获取全部数据

    blues = []#所有红球个数
    reds = []#所有篮球个数
    for row in results :
        red = row[1]
        blue = row[2]
        red_list = red.split(",")#把查询的红球进行以,分割 ["01","02","03","04","05","06"]

        reds.extend(red_list)
        blues.append(blue)

    global reds_count,blues_count
    #统计所有出现红球的个数
    for i in range(1,34):
        reds_count.append(reds.count(str(i).zfill(2)))

    # 统计所有出现篮球的个数
    for i in range(1,17):
        blues_count.append(blues.count(str(i).zfill(2)))

    # redstatistics()
    # bluestatistics()

#添加标签
def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x()-rect.get_width()/4, 1.02*height, "%s"


    
 % int(height))

#红球统计图
def redstatistics():

    width=0.35
    index = np.arange(1, 34)
    y = reds_count
    y1 = np.array(y)
    x1 = index + 1
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    rect = ax1.bar(x1, y1, width, facecolor='#9999ff', edgecolor='white')
    x = [str(i) for i in range(1,34)]
    plt.xticks(index+1+width/20, x)
    plt.ylim(0, 500)
    autolabel(rect)

    ax1.xaxis.set_ticks_position('bottom')
    l1 = ax1.legend(loc=(.02,.92), fontsize=16)
    plt.show()


# 篮球统计图
def bluestatistics():
    width = 0.35
    index = np.arange(1, 17)
    y = blues_count
    y1 = np.array(y)
    x1 = index + 1
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    rect = ax1.bar(x1, y1, width, facecolor='#9999ff', edgecolor='white')
    x = [str(i) for i in range(1, 17)]
    plt.xticks(index + 1 + width / 20, x)
    plt.ylim(0, 500)
    autolabel(rect)

    ax1.xaxis.set_ticks_position('bottom')
    l1 = ax1.legend(loc=(.02, .92), fontsize=16)
    plt.show()


if __name__ == '__main__':
    getdata()
复制代码

最后我们统计红球的柱形图统计图:

在这里插入图片描述
能看出来红球除了24和33,其余出现都超过了400次,出现次数还是比较平均的。
在这里插入图片描述
篮球就8出现的次数比较少一点,也看不出来什么门道。

五、总结

通过统计图也看不出来什么,双色球根本就是毫无规律可言。能学到的知识是爬虫和制图。所以基本靠双色球实现财富自由很难,但运气这种东西谁又说的好呢,所以我给大家精选了一注: 双色球:06,09,12,24,29,31+12,周二晚上21:15开奖。
只要还买得起一注双色球,人生就不至于绝望。最后祝愿大家早日实现财富自由,走上人生巅峰。

欢迎关注我的公众号,我们一起学习。

欢迎关注

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/29987
 
615 次点击