美文网首页小强测试技术与人生杂谈软件测试测试员的那点事
用 Python 撸一个全国疫情地图,其实一点都不难

用 Python 撸一个全国疫情地图,其实一点都不难

作者: 测试帮日记 | 来源:发表于2020-02-11 19:51 被阅读0次

响应号召,宅在家里。修修小技术,水水小代码。加油!热干面!

首先我们假设Python的基本环境已经搭建好了,Let's begin。

一、爬取数据

1)安装常用的python爬虫工具:beautifulsoup4、requests

pipinstall requests

pipinstall beautifulsoup4

2)找一个数据源

在度娘上搜了搜,觉得腾讯的疫情实时追踪非常好。因为他非常友好的把疫情数据公开输出到了console中

网址:https://news.qq.com/zt2020/page/feiyan.htm

       我们在网站页面上单击鼠标右键,选择检查

图1 网站截图

选择Console,我们看到了什么

,这是腾讯大大们抓取处理的所有数据,直接输出到了控制台中。我看可以看到JSON数据格式中:lastUpdateTime是数据的最新更新时间;chinaTotal中是目前的确诊数、疑似数、死亡数、治愈数;chinaDalyList中是1月13日至今的全国总数据;areaTree中是全国详细的数据。

图2 控制台输出截图

图3 areaTree中的全国数据

我们只需要国内的数据,所以只需要第0个children中的数据。

图4 areaTree中的国内数据

现在数据格式知道了,我们来切换到输出这些数据的intro_vp_trim.js脚本中看看这些数据是怎样来的。

图5 数据的获取

       我们可以看到数据是从哪个接口获取的,那么我们只需要在Python中抓取这个接口返回的数据即可,事情变得非常简单了。

3)python抓取数据

importrequestsimportjsonurl ='https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'data = json.loads(requests.get(url=url).json()['data'])china = data['areaTree'][0]['children']

       现在国内的数据就全部在china变量中了,为了方便绘制地图,将各省份的数据提取出来备用。

data = []fori inrange(len(china)):data.append([china[i]['name'],china[i]['total']['confirm']])

二、绘制地图

       我们用pyecharts中的GEO来绘制地图,首先需要安装各种包。

pipinstallpyechartspipinstallecharts-countries-pypkgpipinstallecharts-china-provinces-pypkgpipinstallecharts-china-cities-pypkgpipinstallecharts-china-counties-pypkgpipinstallecharts-china-misc-pypkgpipinstallecharts-united-kingdom-pypkg

如果网速很慢,可以临时用国内的映像,例如:

pipinstall pyecharts -i http://mirrors.aliyun.com/pypi/simple/

pyecharts目前已经更新到了V1版本,并且与V0.5版本不兼容,也不再维护V0.5版本。我们直接用的是V1版本。

       官方网站:https://pyecharts.org/

我们先导入必要的库

importrequestsimportjsonfrompyecharts.chartsimportMap, Geofrompyechartsimportoptionsasoptsfrompyecharts.globalsimportGeoType,RenderType

用全国数据生成个副标题:

china_total ="确诊:"+data['chinaTotal']['confirm'] + \" 疑似:"+data['chinaTotal']['suspect'] + \" 死亡:"+data['chinaTotal']['dead'] +  \" 治愈:"+data['chinaTotal']['heal'] + \" 更新日期:"+data['lastUpdateTime']

设置GEO

geo = (Geo(init_opts = opts.InitOpts(width="1200px",height="600px",bg_color="#404a59",page_title="全国疫情实时报告",renderer=RenderType.SVG,theme="white"))#设置绘图尺寸,背景色,页面标题,绘制类型.add_schema(maptype="china",itemstyle_opts=opts.ItemStyleOpts(color="rgb(49,60,72)",border_color="rgb(0,0,0)"))#中国地图,地图区域颜色,区域边界颜色.add(series_name="geo",data_pair=data,type_=GeoType.EFFECT_SCATTER)#设置地图数据,动画方式为涟漪特效effect scatter.set_series_opts(#设置系列配置label_opts=opts.LabelOpts(is_show=False),#不显示Labeleffect_opts = opts.EffectOpts(scale = 6))#设置涟漪特效缩放比例.set_global_opts(#设置全局系列配置visualmap_opts=opts.VisualMapOpts(min_=0,max_=sum/len(data)),#设置视觉映像配置,最大值为平均值title_opts=opts.TitleOpts(title="全国疫情地图", subtitle=china_total,pos_left="center",pos_top="10px",title_textstyle_opts=opts.TextStyleOpts(color="#fff")),#设置标题,副标题,标题位置,文字颜色legend_opts = opts.LegendOpts(is_show=False),#不显示图例    ))

绘制到html文件

geo.render(path="./html/render.html")

效果预览

图6 效果截图

相关文章

网友评论

    本文标题:用 Python 撸一个全国疫情地图,其实一点都不难

    本文链接:https://www.haomeiwen.com/subject/kaqffhtx.html