IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Python数据分析的过程记录(三) -> 正文阅读

[Python知识库]Python数据分析的过程记录(三)

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

一、需求介绍

在此次的数据分析之中,我们的需求如下:

一行会有十个数据的,我们首先分析前五个数据,这五个数据我们都购买一样的数字,看看这样的方案会有多少次的命中;随后,我们再次购买剩余的五个数据,同样的道理还是这五个数据都购买成一样的,看看这样的方案会有多少次的命中。

最后,综合上述的前五个以及后五个统计出来的结果进行整合,然后发放在一个数据表格中,这个数据表中又有两个表单,第一个表单是前五个数据的分析统计结果;第二个表单是后五个数据的统计分析结果。

二、需求分析

1、获取数据

跟以前一样,还是使用爬虫的方式来进行数据分析的使用的数据的获取。

使用到了 requests 模块。

这个就是一个简单的爬虫了啦。

2、数据预处理

当我们获取到了数据以后呢,需要将 json 数据转换为 字典 类型的数据,然后通过 key 来获取到我们所需要的具体的数据,

在这以后,就为后续的实际分析打好了基础,方便了后续的操作。

3、实现数据分析

根据以上所说的需求介绍,

我们直接按照那个叙述书写代码就可以了啦。

三、代码实现

在这里,代码是举了一个例子,当制作不同的表格的时候,需要修改 target_string
target_string 是我们需要的分析的数字,比如:
01, 02, 03, 等等,
此处的实例是分析 05 这个数字。

如果是分析其他的数字,那么,直接修改这个字符即就是可以实现最终的功能了。

import re
import requests
import xlwt
import json


wb = xlwt.Workbook()  # 创建 excel 表格

list_of_the_dates = []
for i in range(30)[3:]:
    list_of_the_dates.append(f"6-{i + 1}")
list_of_the_dates.append("7-01")
list_of_the_dates.append("7-02")
list_of_the_dates.append("7-03")
list_of_the_dates.append("7-04")
list_of_the_dates.append("7-05")
list_of_the_dates.append("7-06")
list_of_the_dates.append("7-07")
# list_of_the_dates.append("7-08")

target_string = "05"
pos = 1

sh = wb.add_sheet('彩票分析数据处理--1')  # 创建一个 表单
sh.write(0, 0, "时间")
sh.write(0, 1, "1次中的次数")
sh.write(0, 2, "1次中的时间")
sh.write(0, 3, "2次中的次数")
sh.write(0, 4, "2次中的时间")
sh.write(0, 5, "3次中的次数")
sh.write(0, 6, "3次中的时间")
sh.write(0, 7, "4次中的次数")
sh.write(0, 8, "4次中的时间")
sh.write(0, 9, "5次中的次数")
sh.write(0, 10, "5次中的时间")
sh.write(0, 11, "6次中的次数")
sh.write(0, 12, "6次中的时间")
sh.write(0, 13, "7次中的次数")
sh.write(0, 14, "7次中的时间")
sh.write(0, 15, "8次中的次数")
sh.write(0, 16, "8次中的时间")
sh.write(0, 17, "9次中的次数")
sh.write(0, 18, "9次中的时间")
sh.write(0, 19, "10次中的次数")
sh.write(0, 20, "10次中的时间")
sh.write(0, 21, "11次中的次数")
sh.write(0, 22, "11次中的时间")
sh.write(0, 23, "12次中的次数")
sh.write(0, 24, "12次中的时间")
sh.write(0, 25, "13次中的次数")
sh.write(0, 26, "13次中的时间")
sh.write(0, 27, "14次中的次数")
sh.write(0, 28, "14次中的时间")
for date in list_of_the_dates:

    # 每一天都要进行一定的分析了啦

    url = f'https://api.api68.com/pks/getPksHistoryList.do?lotCode=10037&date=2021-0{date}'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59'
    }

    res0 = requests.get(url=url, headers=headers)
    # print(res0)
    new_res = json.loads(res0.content.decode())

    # print(new_res)

    result_of_the_lottery = new_res["result"]["data"]

    times_of_the_data = 1


    dict_1 = {}
    dict_2 = {}
    # 1 是 次数的记录
    # 2 是 时间的记录




    for o in range(1152):
        # 1152 条数据
        data_0_of_the_result = result_of_the_lottery[1151 - o]
        string_0 = data_0_of_the_result["preDrawCode"].split(",")[0: 5]
        # 这里首先使用前五个
        # 前五个

        # print(string_0)
        # 获取到了数据---字符串的形式

        # target_string = "01"
        # 目标字符串

        in_or_not_in_the_data = 0

        # 判断是否中了


        for w in string_0:

            if target_string == w:
                # 中了
                # 随便哪一个中了都可以的
                # 不管哪一个位置中了就是中
                in_or_not_in_the_data = 1
                # 中了以后进行修改,并且要将循环断掉,后面没有必要进行了
                # 置为一,并且要 break
                break
            else:
                # 没中就要继续
                continue

        # if in_or_not_in_the_data
        if in_or_not_in_the_data == 1:

            # 中了

            print(times_of_the_data)

            if f"{times_of_the_data}" in dict_1.keys():
                dict_1[f"{times_of_the_data}"] += 1
            else:
                dict_1[f"{times_of_the_data}"] = 1

            if f"{times_of_the_data}" in dict_2.keys():
                dict_2[f"{times_of_the_data}"].append(
                    data_0_of_the_result["preDrawTime"][10:])
            else:
                dict_2[f"{times_of_the_data}"] = [
                    data_0_of_the_result["preDrawTime"][10:]]

            # times_of_the_data = 1
            times_of_the_data = 1
            # 重置

            # pass

        else:

            # 没中

            times_of_the_data += 1
            # 没中要加一

            # pass

    sh.write(pos, 0, result_of_the_lottery[1151]["preDrawTime"][:10])

    for pl in dict_1.keys():

        if int(pl) >= 1:
            sh.write(pos, 1 + 2 * (int(pl) - 1), dict_1[pl])
        if int(pl) >= 4:
            sh.write(pos, 2 + 2 * (int(pl) - 1), dict_2[pl])
        # 大于 6 的就去进行书写
    pos += 1




    # 另外一个
pos = 1
sh = wb.add_sheet('彩票分析数据处理--2')  # 创建一个 表单
sh.write(0, 0, "时间")
sh.write(0, 1, "1次中的次数")
sh.write(0, 2, "1次中的时间")
sh.write(0, 3, "2次中的次数")
sh.write(0, 4, "2次中的时间")
sh.write(0, 5, "3次中的次数")
sh.write(0, 6, "3次中的时间")
sh.write(0, 7, "4次中的次数")
sh.write(0, 8, "4次中的时间")
sh.write(0, 9, "5次中的次数")
sh.write(0, 10, "5次中的时间")
sh.write(0, 11, "6次中的次数")
sh.write(0, 12, "6次中的时间")
sh.write(0, 13, "7次中的次数")
sh.write(0, 14, "7次中的时间")
sh.write(0, 15, "8次中的次数")
sh.write(0, 16, "8次中的时间")
sh.write(0, 17, "9次中的次数")
sh.write(0, 18, "9次中的时间")
sh.write(0, 19, "10次中的次数")
sh.write(0, 20, "10次中的时间")
sh.write(0, 21, "11次中的次数")
sh.write(0, 22, "11次中的时间")
sh.write(0, 23, "12次中的次数")
sh.write(0, 24, "12次中的时间")
sh.write(0, 25, "13次中的次数")
sh.write(0, 26, "13次中的时间")
sh.write(0, 27, "14次中的次数")
sh.write(0, 28, "14次中的时间")
for date in list_of_the_dates:
    # 每一天都要进行一定的分析了啦

    url = f'https://api.api68.com/pks/getPksHistoryList.do?lotCode=10037&date=2021-0{date}'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59'
    }

    res0 = requests.get(url=url, headers=headers)
    # print(res0)
    # new_res = json.loads(res0.content.decode())

    # print(new_res)

    # result_of_the_lottery = new_res["result"]["data"]
    #
    # times_of_the_data = 1



    dict_1 = {}
    dict_2 = {}
    # 1 是 次数的记录
    # 2 是 时间的记录





    new_res = json.loads(res0.content.decode())

    # print(new_res)

    result_of_the_lottery = new_res["result"]["data"]

    times_of_the_data = 1

    # sh = wb.add_sheet('彩票分析数据处理--2')  # 创建一个 表单

    for o in range(1152):
        # 1152 条数据
        data_0_of_the_result = result_of_the_lottery[1151 - o]
        string_0 = data_0_of_the_result["preDrawCode"].split(",")[5:]
        # 这里其实是从第六个到最后
        # 这里首先使用前五个
        # 前五个

        # print(string_0)
        # 获取到了数据---字符串的形式

        # target_string = "01"
        # 目标字符串

        in_or_not_in_the_data = 0

        # 判断是否中了


        for w in string_0:

            if target_string == w:
                # 中了
                # 随便哪一个中了都可以的
                # 不管哪一个位置中了就是中
                in_or_not_in_the_data = 1
                # 中了以后进行修改,并且要将循环断掉,后面没有必要进行了
                # 置为一,并且要 break
                break
            else:
                # 没中就要继续
                continue

        # if in_or_not_in_the_data
        if in_or_not_in_the_data == 1:

            # 中了

            print(times_of_the_data)


            if f"{times_of_the_data}" in dict_1.keys():
                dict_1[f"{times_of_the_data}"] += 1
            else:
                dict_1[f"{times_of_the_data}"] = 1

            if f"{times_of_the_data}" in dict_2.keys():
                dict_2[f"{times_of_the_data}"].append(
                    data_0_of_the_result["preDrawTime"][10:])
            else:
                dict_2[f"{times_of_the_data}"] = [
                    data_0_of_the_result["preDrawTime"][10:]]

            # times_of_the_data = 1


            times_of_the_data = 1
            # 重置

            # pass

        else:

            # 没中

            times_of_the_data += 1
            # 没中要加一

            # pass


    sh.write(pos, 0, result_of_the_lottery[1151]["preDrawTime"][:10])

    for pl in dict_1.keys():

        if int(pl) >= 1:
            sh.write(pos, 1 + 2 * (int(pl) - 1), dict_1[pl])
        if int(pl) >= 4:
            sh.write(pos, 2 + 2 * (int(pl) - 1), dict_2[pl])
        # 大于 6 的就去进行书写

    # pos += 1

    pos += 1

    # 保存下来了啦

wb.save(f'极速赛车滚雪球数据统计...{target_string}.xls')

# 全部都写完了以后再去进行保存



四、结果呈现

结果的展示如下所示;
图 1、
在这里插入图片描述
在这里插入图片描述

图 2、
在这里插入图片描述
图 3、
在这里插入图片描述
图 4、
在这里插入图片描述

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 14:30:21  更:2021-07-10 14:31:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 18:55:09-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码