代码改变世界

python 爬取新冠肺炎疫情数据

2020-02-21 19:55  默默不语  阅读(1378)  评论(0编辑  收藏  举报

  在腾讯新闻和支付宝中我们都能看到疫情数据,但是支付宝的数据获取难度相对大一些,所以我们获取的腾讯新闻的数据,链接地址:https://news.qq.com/zt2020/page/feiyan.htm?from=timeline&isappinstalled=0

  打开该网页后,我们通过浏览器的开发者工具获取数据(此处我用的edge浏览器)。

  

 

 

  数据为json格式的,通过查看图中所有的接口,终于在上图选中的接口中找到我们想要的数据。

  

 

   我们想要获取中国每个城市名称、确诊人数、治愈人数以及死亡人数来绘制疫情地图。

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import requests
#装了anaconda的可以pip install pyecharts安装pyecharts
# from pyecharts.charts import Map,Geo
# from pyecharts import options as opts
# from pyecharts.globals import GeoType,RenderType
# 绘图包参加网址https://pyecharts.org/#/zh-cn/geography_charts

url="https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
resp=requests.get(url)
html=resp.json()
data=json.loads(html["data"])
print(data["lastUpdateTime"])
china=data["areaTree"][0]["children"]
print(china)
china_total="确诊" + str(data["chinaTotal"]["confirm"])+ "疑似" + str(data["chinaTotal"]["suspect"])+  "死亡" + str(data["chinaTotal"]["dead"]) + "治愈" + str(data["chinaTotal"]["heal"]) + "更新日期" + data["lastUpdateTime"]
print(china_total)
data=[]
for i in range(len(china)):
    data.append([china[i]["name"], china[i]["total"]["confirm"]])
print(data)

  运行结果:

 

  这样我们就获取到了想要的数据啦,如果想要获取其他数据,只需要修改该字段名称即可!