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()
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")
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)
new_res = json.loads(res0.content.decode())
result_of_the_lottery = new_res["result"]["data"]
times_of_the_data = 1
dict_1 = {}
dict_2 = {}
for o in range(1152):
data_0_of_the_result = result_of_the_lottery[1151 - o]
string_0 = data_0_of_the_result["preDrawCode"].split(",")[0: 5]
in_or_not_in_the_data = 0
for w in string_0:
if target_string == w:
in_or_not_in_the_data = 1
break
else:
continue
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
else:
times_of_the_data += 1
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])
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)
dict_1 = {}
dict_2 = {}
new_res = json.loads(res0.content.decode())
result_of_the_lottery = new_res["result"]["data"]
times_of_the_data = 1
for o in range(1152):
data_0_of_the_result = result_of_the_lottery[1151 - o]
string_0 = data_0_of_the_result["preDrawCode"].split(",")[5:]
in_or_not_in_the_data = 0
for w in string_0:
if target_string == w:
in_or_not_in_the_data = 1
break
else:
continue
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
else:
times_of_the_data += 1
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])
pos += 1
wb.save(f'极速赛车滚雪球数据统计...{target_string}.xls')
四、结果呈现
结果的展示如下所示; 图 1、  
图 2、  图 3、  图 4、 
|