当前位置: 首页 > 科技观察

教你用Python获取和可视化新冠疫情数据

时间:2023-03-16 22:50:57 科技观察

大家好。我的名字是“小孩子i”。在前言中,不知道大家有没有看到这样一句话:“中国(疫苗研发)非常困难,因为我们没有办法在中国做三期临床试验,因为没有病人。“这句话是中国工程院院士钟南山说的。在上海科技大学2021届毕业典礼上发表。这句话在网络上广为流传,被广大网友称为“凡尔赛”演讲。今天我们就用数据来看看这句话是不是《凡尔赛宫》这款游戏。在开始之前,先说说今天要用到的python库吧!1、数据获取部分请求lxmljsonopenpyxl2。数据可视化部分pandaspyecharts(可视化库)及以上库可以在网上下载:pipinstllxxps:如果下载速度太慢可以使用国内镜像,使用命令,例如:pipinstallxx(库名)-ihttps://pypi.tuna.tsinghua.edu.cn/simplegevent(清华镜像)下面一起进入今天的代码部分吧!!!数据获取目标地址:https://voice.baidu.com/act/newpneumonia/newpneumonia输入目标地址,我们可以看到如下:现在我们来分析一下网页结构,找到我们要爬取的数据如下:现在我们find接下来就是通过Python获取想要的页面数据。上面代码:importrequestsfromlxmlimportetreeimportjsonimportopenpyxl#Generalcrawlerurl='https://voice.baidu.com/act/newpneumonia/newpneumonia'headers={"User-Agent":".....(替换成你自己的)"}response=requests.get(url=url,headers=headers).text#使用xpath时使用树形html=etree.HTML(response)#使用xpath获取我们之前查到的页面的json数据打印出来看看json_text=html.xpath('//script[@type="application/json"]/text()')json_text=json_text[0]#print(json_text)之后我们来分析json数据,代码:#用python的本地库转换json数据result=json.loads(json_text)#print(result)#打印出转换后的对象,可以看到我们要的数据必须在组件对应的值下,所以现在我们取值result=result["componentent"]#再次打印看结果#print(result)#获取当前国内数据result=result[0]['caseList']#print(result)然后我们将获取到的数据保存到excel中,上面的代码:#创建工作簿wb=openpyxl.Workbook()#创建工作表ws=wb.active#设置表的标题ws.title="国内疫情"#写表头ws.append(["省份","累计confirmedcases","Death","Cure"])#获取各省的数据写入forlineinresult:line_name=[line["area"],line["confirmed"],line["died"],line["crued"]]foreleinline_name:ifele=='':ele=0ws.append(line_name)#Savetoexcelwb.save('./china.xlsx')最后我们看看得到的数据是什么样子的,如图:emmmmm,终于完成了数据获取部分,接下来就是数据可视化的第二部分了!!!这次我们数据可视化使用的库是pyecharts中的Map,先展示下使用的库这个可视化#Visualizationpartimportpandasaspdrompyecharts.chartsimportMap,Pagefrompyechartsimportoptionsasopts首先我们需要使用pandas库获取我们刚才爬取的数据,上面的代码:#设置列对齐pd.set_option('display.unicode.ambiguous_as_wide',True)pd.set_option('display.unicode.east_asian_width',True)#打开文件df=pd.read_excel('china.xlsx')#统计省份data2=df['province']data2_list=list(data2)data3=df['累计诊断']data3_list=list(data3)data4=df['死亡']data4_list=list(data4)data5=df['cure']data5_list=list(data5)然后我们做数据可视化,在我国地图上的每个省都会显示对应的值。我们以疫情以来的治愈人数为例,上面的代码:c=(Map().add("cure",[list(z)forzinzip(data2_list,data5_list)],"china").set_global_opts(title_opts=opts.TitleOpts(),visualmap_opts=opts.VisualMapOpts(max_=200),))c.render()当然,只有一种治愈情况并不能说明什么,所以我们将三种情况都用这种形式展示出来,上面的代码:a=(Map().add("累计诊断",[list(z)forzinzip(data2_list,data3_list)],"china").set_global_opts(title_opts=opts.TitleOpts(),visualmap_opts=opts.VisualMapOpts(max_=200),))b=(Map().add("死亡",[list(z)forzinzip(data2_list,data4_list)],"china").set_global_opts(title_opts=opts.TitleOpts(),visualmap_opts=opts.VisualMapOpts(max_=200),))c=(Map().add("Cure",[list(z)forzinzip(data2_list,data5_list)],"china").set_global_opts(title_opts=opts.TitleOpts(),visualmap_opts=opts.VisualMapOpts(max_=200),))page=Page(layout=Page.DraggablePageLayout)page.add(a,b,c,)#Mr.生成render.html文件page.render()当然,如果直接运行代码,显示的地图不是这样的。这是通过后期排版来完成的,那么最后再说说如何排版吧。首先,运行上面的代码后,会生成一个render.html文件。打开文件后,你可以根据自己的喜好调整整个页面的布局,然后点击左上角的“SaveConfig”保存json文件。进入与render.html文件相同的路径,最后运行代码:#完成上一步后,注释掉page.render()这一行#然后在Page.save_resize_html("render.html",cfg_file="chart_config.json",dest="my_test.html")这样以后会生成一个my_test.html文件,就是我们上面展示的。结论以上是我们这次的结果。从数据采集到数据可视化,怎么说pyecharts还有其他强大的可视化功能。