快好知 kuaihz订阅观点

 

我用Python制作了全国疫情地图,其实一点都...

这里是码农充电第一站,回复“666”,获取一份专属大礼包

真爱,请设置“星标”或点个“在看”

来源:水代码工作室

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

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

一、爬取数据

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

pip install requests

pip install 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抓取数据

import requestsimport jsonurl = "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 = []for i in range(len(china)):    data.append([china[i]["name"],china[i]["total"]["confirm"]])

二、绘制地图

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

pip install pyechartspip install echarts-countries-pypkgpip install echarts-china-provinces-pypkgpip install echarts-china-cities-pypkgpip install echarts-china-counties-pypkgpip install echarts-china-misc-pypkgpip install echarts-united-kingdom-pypkg

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

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

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

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

       我们先导入必要的库

import requestsimport jsonfrom pyecharts.charts import Map, Geofrom pyecharts import options as optsfrom pyecharts.globals import GeoType,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),#不显示Label effect_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  Python词条  
综合

 利用pxssh暴力破解ssh密码

在前面我们利用Pexpect实现了SSH登录。不过这种方式已经有一个现成的包做好了。它叫Pxssh 首先用一个小例子来感受一下它的用法:from pexpect...(展开)