|
| 1 | +import time, json, requests |
| 2 | + |
| 3 | +# 腾讯疫情实时数据数据 URL |
| 4 | +url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&callback=&_=%d'%int(time.time()*1000) |
| 5 | +# 加载 JSON 数据并解析 |
| 6 | +data = json.loads(requests.get(url=url).json()['data']) |
| 7 | +# 打印数据输出数据 |
| 8 | +print(data) |
| 9 | +print(data.keys()) |
| 10 | + |
| 11 | +# 统计省份信息(34个省份 湖北 广东 河南 浙江 湖南 安徽....) |
| 12 | +num_area = data['areaTree'][0]['children'] |
| 13 | +print(len(num_area)) |
| 14 | +# 遍历所有数据后输出,直到输出结束 |
| 15 | +for item in num_area: |
| 16 | + print(item['name'],end=" ") |
| 17 | +else: |
| 18 | + print("\n") |
| 19 | + |
| 20 | +# 解析所有确诊数据 |
| 21 | +all_data = {} |
| 22 | +for item in num_area: |
| 23 | + # 输出省市名称 |
| 24 | + if item['name'] not in all_data: |
| 25 | + all_data.update({item['name']:0}) |
| 26 | + #输出省市对应的数据 |
| 27 | + for city_data in item['children']: |
| 28 | + all_data[item['name']] +=int(city_data['total']['confirm']) |
| 29 | +# 输出结果 |
| 30 | +print(all_data) |
| 31 | + |
| 32 | +#-------------------------------------------------------------------------------- |
| 33 | + |
| 34 | +# 使用 Matplotlib 绘制全国确诊病例柱状图 |
| 35 | +import matplotlib.pyplot as plt |
| 36 | +import numpy as np |
| 37 | + |
| 38 | +plt.rcParams['font.sans-serif'] = ['SimHei'] #正常显示中文标签 |
| 39 | +plt.rcParams['axes.unicode_minus'] = False #正常显示负号 |
| 40 | + |
| 41 | +#获取数据 |
| 42 | +names = all_data.keys() |
| 43 | +nums = all_data.values() |
| 44 | +print(names) |
| 45 | +print(nums) |
| 46 | + |
| 47 | +# 绘图 |
| 48 | +plt.figure(figsize=[11,7]) |
| 49 | +plt.bar(names, nums, width=0.8, color='purple') |
| 50 | + |
| 51 | +# 设置标题 |
| 52 | +plt.xlabel("地区", fontproperties='SimHei', size=15) |
| 53 | +plt.ylabel("人数", fontproperties='SimHei', rotation=90, size=12) |
| 54 | +plt.title("全国疫情确诊图", fontproperties='SimHei', size=16) |
| 55 | +plt.xticks(list(names), fontproperties='SimHei', rotation=-60, size=10) |
| 56 | + |
| 57 | +# 显示数字 |
| 58 | +for a, b in zip(list(names), list(nums)): |
| 59 | + plt.text(a, b, b, ha='center', va='bottom', size=6) |
| 60 | + |
| 61 | +# 图形展示 |
| 62 | +plt.show() |
0 commit comments