Python数据分析的过程记录(二)

Python数据分析的过程记录(二)Python数据分析的过程记录(二)文章目录Python数据分析的过程记录(二)一、需求介绍二、需求分析三、代码实现一、需求介绍二、需求分析三、代码实现

大家好,又见面了,我是你们的朋友全栈君。

Python数据分析的过程记录(二)

一、需求介绍

在本篇博文中,我们在使用另外一种方法来进行彩票的数据分析,这种方法我们称之为滚雪球,即就是说需要不断地迭代来进行彩票的购买与预计估算等操作。

我们要买的彩票名称叫“极速赛车”

链接例如:

https://www.1681160.com/api/pks/getPksHistoryList.do’?date=2021-07-05&lotCode=10037

这个是数据存储的网页的链接,如果想更严谨一点的话,他是Ajax的模式。

二、需求分析

1、简介

根据上面的需求介绍,我们大致明白了要干什么。

2、获取数据

不管进行怎样的分析,我们都需要进行数据的获取或者说是提取,在这里呢,我们就采用爬虫的方式来进行数据的获取。

我们使用了Python中的requests模块来进行爬虫的操作,从而获取到了,不同天的彩票的基本数据:

2.1、时间信息

2.2、开奖的数字信息

2.3、其他需要的信息等

3、数据分析的方案

我们首先定义一个列表,这个列表之中会记录一些数据,我们根据这个列表中的数据来进行下一次的彩票的购买,具体的方案如下:
1、我们购买的数据应该要满足,他们全都在这个列表之中,即就是说,列表之中的数据我们都要购买,不管中不中我们都买:
2、如果中了,那么我们就把这个数据从当前的列表里面剔除出去,如果没有中,那么我们就把这个数据加入到列表里面去,;
3、如上所述,我们依次的向下进行推进着进行购买彩票。

这就是我们的一个新的方案。

下面就是具体的代码实现了。

三、代码实现

在代码里面有一些注释,根据这些注释以及相关代码是可以理解具体的意图的啦。

import re
import requests
import xlwt
import json

""" 数字 位置 天数 数据 """

# 时间列表
date_list = ["7-01", "7-02", "7-03"]
for i in range(30):
    date_list.append(f"6-{ 
     i + 1}")


for number in ["01", "02", "03", "04", "05", 
               "06", "07", "08", "09", "10"]:

    wb = xlwt.Workbook()  # 创建 excel 表格
    # 每一个数字对应于一个 excel 表格

    for position_of_the_lottery in range(10):

        sh = wb.add_sheet(f'彩票分析数据处理{ 
     position_of_the_lottery}')
        # 创建一个 表单
        """ 表头 """
        sh.write(0, 0, "日期")
        sh.write(0, 1, "48次中的数目")
        sh.write(0, 2, "48次中的时间")
        sh.write(0, 3, "49次中的数目")
        sh.write(0, 4, "49次中的时间")
        sh.write(0, 5, "50次中的数目")
        sh.write(0, 6, "50次中的时间")
        sh.write(0, 7, "51次中的数目")
        sh.write(0, 8, "51次中的时间")
        sh.write(0, 9, "52次中的数目")
        sh.write(0, 10, "52次中的时间")
        sh.write(0, 11, "53次中的数目")
        sh.write(0, 12, "53次中的时间")
        sh.write(0, 13, "54次中的数目")
        sh.write(0, 14, "54次中的时间")
        sh.write(0, 15, "55次中的数目")
        sh.write(0, 16, "55次中的时间")
        sh.write(0, 17, "56次中的数目")
        sh.write(0, 18, "56次中的时间")
        sh.write(0, 19, "57次中的数目")
        sh.write(0, 20, "57次中的时间")
        sh.write(0, 21, "58次中的数目")
        sh.write(0, 22, "58次中的时间")
        sh.write(0, 23, "59次中的数目")
        sh.write(0, 24, "59次中的时间")
        sh.write(0, 25, "60次中的数目")
        sh.write(0, 26, "60次中的时间")
        sh.write(0, 27, "61次中的数目")
        sh.write(0, 28, "61次中的时间")
        sh.write(0, 29, "62次中的数目")
        sh.write(0, 30, "62次中的时间")
        sh.write(0, 31, "63次中的数目")
        sh.write(0, 32, "63次中的时间")
        sh.write(0, 33, "64次中的数目")
        sh.write(0, 34, "64次中的时间")
        # 绘制表头

        excel_position_of_the_mouse = 1
        # 这个应该是一个表一个
        # 所以说哦 需要放在外面


        for date in date_list:

            url = 'https://www.1681160.com/api/pks/getPksHistoryList.do' \
                  f'?date=2021-0{ 
     date}&lotCode=10037'
            headers = { 
   
                'User-Agent':
                    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                    '(KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64',
                'X-Requested-With':
                    'XMLHttpRequest'
            }  # Ajax 请求
            response_0 = requests.get(url=url, headers=headers)
            new_response_0 = json.loads(response_0.content.decode())["result"]["data"]
            # 获取数据

            # 初始化
            # global
            times_of_the_number_of_coming = 1  # 初始化数目

            # 每一天都需要进行初始化 date

            dict_of_times = { 
   }  # 次数统计 dict
            dict_of_t = { 
   }  # 时间统计 dict

            # excel_position_of_the_mouse = 1

            for the_data_of_the_daily_lottery in range(1152):

                # 1152 条数据

                a_result = new_response_0[1151 - the_data_of_the_daily_lottery][
                               "preDrawCode"][0 + 3 * position_of_the_lottery:
                                              2 + 3 * position_of_the_lottery]

                if a_result == number:  # 如果得到的结果 == 开始的数字
                    # 如果相等,那就是命中了

                    if f'{ 
     times_of_the_number_of_coming}' in dict_of_times.keys():
                        dict_of_times[f'{ 
     times_of_the_number_of_coming}'] += 1
                    else:
                        dict_of_times[f'{ 
     times_of_the_number_of_coming}'] = 1

                    if f'{ 
     times_of_the_number_of_coming}' in dict_of_t.keys():
                        dict_of_t[f'{ 
     times_of_the_number_of_coming}'].append(
                            new_response_0[1151 - the_data_of_the_daily_lottery][
                                "preDrawTime"][10:]
                        )
                    else:
                        dict_of_t[f'{ 
     times_of_the_number_of_coming}'] = [
                            new_response_0[1151 - the_data_of_the_daily_lottery][
                                "preDrawTime"][10:]
                        ]

                    print(times_of_the_number_of_coming)
                    times_of_the_number_of_coming = 1

                    # 重置
                else:
                    # 如果不相等那就是没有命中
                    # 那么,我们需要自加一
                    
                    times_of_the_number_of_coming += 1  # 自加一

            # 书写时间

            sh.write(excel_position_of_the_mouse, 0,
                     new_response_0[0]["preDrawTime"][:10])
            # 时间写在第一列上面,时间随便取一个就可以了


            for e in dict_of_times.keys():
                if int(e) >= 48:
                    sh.write(excel_position_of_the_mouse,
                             1 + (int(e) - 48) * 2,
                             dict_of_times[e])
                    sh.write(excel_position_of_the_mouse,
                             2 + (int(e) - 48) * 2,
                             dict_of_t[e])
                    # 写入次数以及时间
                else:
                    continue
                    # 小于 48 的不记入数据里面

            excel_position_of_the_mouse += 1
            # 当搞完一个以后,需要改变一下位置


    # 保存
    wb.save(f'极速赛车滚雪球数据统计-{ 
     number}.xls')

四、成品结果展示

最后我们的成果是一个文件夹,
里面有十个excel表格,因为有十个数字需要进行滚雪球方式的寻找与查看。

同时,每一个excel表格中有十个表单,因为每一个数字都需要有十个位置需要统计。

成果的截图如下所示;

图1
在这里插入图片描述

图2
在这里插入图片描述

图3

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160366.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 接口定义

    接口定义1、什么是接口接口一般来讲分为两种程序内部的接口:方法与方法、模块与模块之间的交互,程序内部抛出的接口,如登录发帖,发帖就必须要登录,如果不登录不能发帖,发帖和登录这两个模块之间就要有交互,就会抛

    2022年7月1日
    28
  • 简述TCP的三次握手和四次挥手过程[通俗易懂]

    简述TCP的三次握手和四次挥手过程[通俗易懂]①TCP是一种精致的,可靠的字节流协议。②在TCP编程中,三路握手一般由客户端(Client)调用Connent函数发起。③TCP3次握手后数据收发通道即打开(即建立了连接)。④简述三路握手过程:图.TCP三次握手(1)第一次握手:Client将标志位SYN置为1,随机产生一个值s…

    2022年6月2日
    28
  • the beginning of_The king

    the beginning of_The kingThe 2016 Asia Regional Contest, Tsukuba Quality of Check Digits Gym – 101158B

    2022年4月20日
    32
  • 遗传算法(Genetic Algorithm)详解与实现「建议收藏」

    遗传算法(Genetic Algorithm)详解与实现「建议收藏」遗传算法(GeneticAlgorithm,GA)是受自然进化原理启发的一系列搜索算法。通过模仿自然选择和繁殖的过程,遗传算法可以为涉及搜索、优化和学习的各种问题提供高质量的解决方案。同时,它们类似于自然进化,因此遗传算法可以克服传统搜索和优化算法遇到的一些障碍,尤其是对于具有大量参数和复杂数学表示形式的问题。

    2025年5月25日
    0
  • error: #5: cannot open source input file “xxx.h“: No such file or directory

    error: #5: cannot open source input file “xxx.h“: No such file or directoryerror:#5:cannotopensourceinputfile”xxx.h”:Nosuchfileordirectory关于新加一个.c文件编译后为何报错,是因为.h没有加进工程里面。只需要几步,然后找到.h文件所处的文件添加进去,编译,就不会报错了。…

    2022年7月14日
    13
  • 学了Java才搞懂JMeter测试计划

    学了Java才搞懂JMeter测试计划

    2020年11月20日
    197

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号