通过浏览器“审查元素”查看源代码及“网络”反馈的消息,如下图所示:
对应的响应信息如下所示:
使用json格式化,将获取的响应信息展开:
截取出需要的数据字段标识:
流程图设计
代码和程序设计
import time, json, requests
# 抓取腾讯疫情实时json数据
url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000)
data = json.loads(requests.get(url=url).json()['data'])
#print(data)
#print(data.keys())
num = data['areaTree'][0]['children']
# 解析数据(确诊 疑似 死亡 治愈)
total_data = {}
for item in num:
if item['name'] not in total_data:
total_data.update({item['name']:0})
# print(item['name'])
for city_data in item['children']:
# print(city_data )
total_data[item['name']] += city_data['total']['confirm']
# 省份名称
names = list(total_data.keys())
# 确诊数据
num1 = list(total_data.values())
for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]="确诊人数: "+str(total_data[item['name']])
# print(total_data)
#解析疑似数据
total_suspect_data = {}
for item in num:
if item['name'] not in total_suspect_data:
total_suspect_data.update({item['name']: 0})
for city_data in item['children']:
total_suspect_data[item['name']] += int(city_data['total']['suspect'])
# 疑似数据
num2 = list(total_suspect_data.values())
for item in num:
total_data[item['name']]=total_data[item['name']]+", 疑似病例:"+str(total_suspect_data[item['name']])
# print(total_data)
# 解析死亡数据
total_dead_data = {}
for item in num:
if item['name'] not in total_dead_data:
total_dead_data.update({item['name']: 0})
for city_data in item['children']:
total_dead_data[item['name']] += int(city_data['total']['dead'])
# 死亡数据
num3 = list(total_dead_data.values())
for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]=total_data[item['name']]+", 死亡数据:"+str(total_dead_data[item['name']])
# 解析治愈数据
total_heal_data = {}
for item in num:
if item['name'] not in total_heal_data:
total_heal_data.update({item['name']: 0})
for city_data in item['children']:
total_heal_data[item['name']] += int(city_data['total']['heal'])
# 治愈数据
num4 = list(total_heal_data.values())
for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]=total_data[item['name']]+", 治愈数据:"+str(total_heal_data[item['name']])
# 解析新增确诊数据
total_new_data = {}
for item in num:
if item['name'] not in total_new_data:
total_new_data.update({item['name']: 0})
for city_data in item['children']:
total_new_data[item['name']] += int(city_data['today']['confirm']) # today
# 新增确诊病例
num5 = list(total_new_data.values())
for item in num:
# print("...", total_data[item['name']])
total_data[item['name']]=total_data[item['name']]+", 新增确诊人数:"+str(total_new_data[item['name']])
# print(total_data)
for i,j in total_data.items():
print(i," ",j)
运行结果
由于具有三类型的数据类型,为了将数据写入再同一个表中,使用Seaborn进行图像绘制
数据写入,接上代码,这里因为武汉的数据较大,会影响到其他的图表,我直接跳过了湖北,湖北为i=13号。
print(names)
print(num1)
print(num2)
print(num3)
print(num4)
print(num5)
# 获取当前日期命名
n = time.strftime("%Y-%m-%d") + "-data_2.csv"
fw = open(n, 'w', encoding='utf-8')
fw.write('省份,类型,人数\n')
i = 0
while i < len(names):
if (i!=13):
fw.write(names[i] + ',累计确诊,' + str(num1[i]) + '\n')
fw.write(names[i] + ',累计死亡,' + str(num3[i]) + '\n')
fw.write(names[i] + ',累计治愈,' + str(num4[i]) + '\n')
fw.write(names[i] + ',新增确诊,' + str(num5[i]) + '\n')
i = i + 1
else:
print("跳过武汉!")
i = i + 1
else:
print("Over !")
fw.close()
图表绘制:
# 调用Seaborn绘制柱状图
import time
import matplotlib
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
n=time.strftime("%Y-%m-%d")+"-data_2.csv"
data=pd.read_csv(n)
# 设置窗口
fig,ax=plt.subplots(1,1)
print(data['省份'])
# 设置绘图风格及字体
sns.set_style("whitegrid",{'font.sans-serif':['simhei','Arial']})
# 绘制柱状图
g=sns.barplot(x='省份',y='人数',hue="类型",data=data,ax=ax,palette=sns.color_palette("hls",8))
# 设置Axes的标题
ax.set_title('湖北外-中国33省份累计病例对比图')
# 设置坐标轴文字方向
ax.set_xticklabels(ax.get_xticklabels(),rotation=-60)
# 设置坐标轴刻度的字体大小
ax.tick_params(axis='x',labelsize=8)
ax.tick_params(axis='y',labelsize=8)
plt.show()
结果