今天在朋友圈看到一个同学写的python获取全国疫情地图,我觉得挺有意思的,这个分享给大家,如果喜欢python的,可以通过这个作为入门手段。
1、安装python
看这个链接来安装,非常的小白
https://www.liaoxuefeng.com/wiki/1016959663602400/1016959856222624
2、获取代码
代码是这位小哥写的,我是拿来主义,我把代码上传了github,链接如下。
https://github.com/weiqifa0/yiqingditu
import time
import json
import requests
import jsonpath
from pyecharts.charts import Map
import pyecharts.options as opts# 全国疫情地区分布(各省确诊病例)
def catch_cn_disease_dis():timestamp = '%d'%int(time.time()*1000)url_area = ('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5''&callback=&_=') + timestampworld_data = json.loads(requests.get(url=url_area).json()['data'])china_data = jsonpath.jsonpath(world_data,expr='$.areaTree[0].children[*]')ls_province_names = jsonpath.jsonpath(china_data, expr='$[*].name')ls_confirm_vals = jsonpath.jsonpath(china_data, expr='$[*].total.confirm')ls_province_confirm = list(zip(ls_province_names, ls_confirm_vals,))return ls_province_confirm, world_datals_province_cfm, dic_world_data = catch_cn_disease_dis()
print(ls_province_cfm)# 绘制全国疫情地图
def map_cn_disease_dis() -> Map:c = (Map().add('中国', ls_province_cfm, 'china').set_global_opts(title_opts=opts.TitleOpts(title='全国新型冠状病毒疫情地图(确诊数)'),visualmap_opts=opts.VisualMapOpts(is_show=True,split_number=6,is_piecewise=True, # 是否为分段型pos_top='center',pieces=[{'min': 10000, 'color': '#7f1818'}, #不指定 max{'min': 1000, 'max': 10000},{'min': 500, 'max': 999},{'min': 100, 'max': 499},{'min': 10, 'max': 99},{'min': 0, 'max': 5} ],),))return c
map_cn_disease_dis().render('全国疫情地图.html')
3、运行
第一次运行的时候,肯定会出现有库没有安装,直接运行 pip install xx
后面xx是你要安装的库的名字就好了,不要太简单了。
4、运行
最后生产一个html,双击运行即可
扫码或长按关注
回复「 篮球的大肚子」进入技术群聊