新冠病毒数据分析(1)----爬虫技术获取疫情数据


1、实时数据网址
新型冠状病毒肺炎疫情实时追踪
 
2、抓包
使用浏览器自带的抓包功能,找到返回的数据,确定格式是json格式。
 
3、返回json数据地址
4、处理json数据
通过传入的网址,处理json数据,返回字典对象
def getData(url):
    html=requests.get(url)
    #获取返回的json数据
    data_json=html.json()
    data_dict=json.loads(data_json['data'])
    return data_dict
字典对象格式如下:
5、获取数据获取时间
 
    #新冠数据获取时间  
    print("数据获取时间:{}".format(data_dict['lastUpdateTime']  ))
6、获取各国新冠数据
分析字典中键和值,
代码如下
 
def getAllCountry():
    
    for area in data_dict['areaTree']:
        print("{}新冠详情\r\n确诊{}\r\n治愈{}\r\n死亡{}\r\n疑似{}".format(area['name'], \
          area['total']['confirm'],\
          area['total']['heal'],\
         area['total']['dead'],\
         area['total']['suspect'],\
         ))
执行结果
7、获取中国各省新冠数据
 
分析字典中键和值
代码如下
def getChinaProvince():
    for province in data_dict['areaTree'][0]['children']:
        print("{}新冠详情\r\n确诊{}\r\n治愈{}\r\n死亡{}\r\n疑似{}".format(province['name'], \
        province['total']['confirm'],\
        province['total']['heal'],\
        province['total']['dead'],\
        province['total']['suspect'],\
         ))
执行结果
8、获取中国各市新冠数据
分析字典中键和值
代码如下
def getChinaCity():
    #for i in len(data_dict['areaTree'][0]['children']):
    for i in range(0, len(data_dict['areaTree'][0]['children'])):
        print("\r\n{}省".format(data_dict['areaTree'][0]['children'][i]['name']))
        for city in data_dict['areaTree'][0]['children'][i]['children']:
            print("{}新冠详情\r\n确诊{}\r\n治愈{}\r\n死亡{}\r\n疑似{}".format(city['name'], \
        city['total']['confirm'],\
        city['total']['heal'],\
        city['total']['dead'],\
        city['total']['suspect'],\
         ))
运行结果如下:
 
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM