一、 json模块
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
json.loads()
:将json格式的str转化成python的数据格式;json.loads()
:将python的数据格式(字典或列表)转化成json格式;
# 如何将json数据解析成我们所熟悉的Python数据类型?
import json
# 将json格式的str转化成python的数据格式:字典
dic = json.loads('{"name":"Tom","age":23}')
res = json.loads('["name","age","gender"]')
print(f'利用loads将json字符串转化成Python数据类型{dic}',type(dic))
print(f'利用loads将json字符串转化成Python数据类型{res}',type(res))
dics = {"name":"Tom","age":23}
result = json.dumps(dics)
print(type(result))
result
二、通过Python实现疫情地图可视化
# 1.数据的获取(基于request模块)
import requests
import json
# 国内疫情数据
China_url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
headers = {
# 浏览器伪装
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
'referer': 'https://news.qq.com/',
}
# 发起get请求,获取响应数据
response = requests.get(China_url,headers=headers).json()
data = json.loads(response['data'])
# 保存数据
with open('./2021-02-03国内疫情.json','w',encoding='utf-8') as f:
# 不采用ASCII编码
f.write(json.dumps(data,ensure_ascii=False,indent=2))
爬取的数据保存格式为json,开头的部分数据如下:
2.将json格式的数据保存到Excel
无论是json数据存储的,还是Python的基本数据类型存储的,对于数据分析都不是很友好,所以我们可以将其数据存储类型转化为pandas的DataFrame类型,因为DataFrame和Excel可以更好的相互转换。
# 读取文件
with open('./2021-02-03国内疫情.json','r',encoding='utf-8') as f:
data = f.read()
# 将数据转成Python数据格式(字符串转换为字典)
data = json.loads(data)
# 1.获取数据最新的更新时间
lastUpdateTime = data['lastUpdateTime']
# 2.获取国内的所有疫情相关的数据
chinaAreaDict = data['areaTree']
# 3.获取省级数据
provinceList = chinaAreaDict[0]['children']
# 将国内数据按城市封装
china_citylist = []
for x in range(len(provinceList)):
province = provinceList[x]['name']
province_list = provinceList[x]['children']
for y in range(len(province_list)):
# 每一个地级市的数据
city = province_list[y]['name']
total = province_list[y]['total']
today = province_list[y]['today']
china_dict = {'province':province,
'city':city,
'total':total,
'today':today}
china_citylist.append(china_dict)
china_citylist
生成的数据模式如下:
将以上的数据进行处理,获得Excel表一样规范的数据格式。
import pandas as pd
chinaTotalData = pd.DataFrame(china_citylist)
# 将整体数据chinaTotalData中的today和total数据添加到DataFrame中
# 处理total字典里面的各个数据项
# ======================================================================
confirmlist = []
suspectlist = []
deadlist = []
heallist = []
deadRatelist = []
healRatelist = []
# print(chinaTotalData['total'].values.tolist()[0])
for value in chinaTotalData['total'].values.tolist():
confirmlist.append(value['confirm'])
suspectlist.append(value['suspect'])
deadlist.append(value['dead'])
heallist.append(value['heal'])
deadRatelist.append(value['deadRate'])
healRatelist.append(value['healRate'])
chinaTotalData['confirm'] = confirmlist
chinaTotalData['suspect'] = suspectlist
chinaTotalData['dead'] = deadlist
chinaTotalData['heal'] = heallist
chinaTotalData['deadRate'] = deadRatelist
chinaTotalData['healRate'] = healRatelist
# ===================================================================
# 创建全国today数据
today_confirmlist = []
today_confirmCutslist = []
for value in chinaTotalData['today'].values.tolist():
today_confirmlist.append(value['confirm'])
today_confirmCutslist.append(value['confirmCuts'])
chinaTotalData['today_confirm'] = today_confirmlist
chinaTotalData['today_confirmCuts'] = today_confirmCutslist
# ==================================================================
# 删除total、today两列
chinaTotalData.drop(['total','today'],axis=1,inplace=True)
chinaTotalData.head()
# 将其保存到Excel中
chinaTotalData.to_excel('2021-02-03国内疫情.xlsx',index=False)
处理好的数据结构如下表:
3.应用pyecharts进行数据可视化
pyecharts
是一款将python与echarts结合的强大的数据可视化工具。绘制出来的图比Python的Matplotlib简单美观。使用之前需要在Python环境中按照pycharts。在终端中输入命令:pip install pyecharts
利用pyecharts绘制疫情地图
根据上面的疫情数据,我们可以利用其画出全国的疫情地图
在绘制前,我们需要安装echarts的地图包(可根据不同的地图需求进行安装)
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-misc-pypkg
pip install echarts-china-countries-pypkg
pip install echarts-united-kingdom-pypkg
# 导入对应的绘图工具包
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Map
df = pd.read_excel('./2021-02-03国内疫情.xlsx')
# 1.根据绘制国内总疫情图(确诊)
data = df.groupby(by='province',as_index=False).sum()
data_list = list(zip(data['province'].values.tolist(),data['confirm'].values.tolist()))
# 数据格式[(黑龙江,200),(吉林,300),...]
def map_china() -> Map:
c = (
Map()
.add(series_name="确诊病例",data_pair=data_list,maptype='china')
.set_global_opts(
title_opts = opts.TitleOpts(title='疫情地图'),
visualmap_opts=opts.VisualMapOpts(is_piecewise=True,
pieces = [{"max":9, "min":0, "label":"0-9","color":"#FFE4E1"},
{"max":99, "min":10, "label":"10-99","color":"#FF7F50"},
{"max":499, "min":100, "label":"100-4999","color":"#F08080"},
{"max":999, "min":500, "label":"500-999","color":"#CD5C5C"},
{"max":9999, "min":1000, "label":"1000-9999","color":"#990000"},
{"max":99999, "min":10000, "label":"10000-99999","color":"#660000"},]
)
)
)
return c
d_map = map_china()
d_map.render("mapEchrts.html")
最终的运行效果如下: