这篇文章给大家分享的是有关python如何爬取疫情数据的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
为郊区等地区用户提供了全套网页设计制作服务,及郊区网站建设行业解决方案。主营业务为成都网站设计、成都网站制作、郊区网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!疫情数据
程序源码
// An highlighted block import requests import json class epidemic_data(): def __init__(self, province): self.url = url self.header = header self.text = {} self.province = province # self.r=None def down_page(self): r = requests.get(url=url, headers=header) self.text = r.text # self.r = r def parse_page(self): # print(type(self.r.json()['data'])) # 因为解析数据为 data 前有一个"",所以数据类型为string data_str = json.loads(self.text)['data'] #sring # print(type(data_dict)) # print(type(data_dict['data'])) # 将str 转化为对象 data_json = json.loads(data_str) data_tree_dict = data_json['areaTree'][0]['children'] # 取中国的省列表 prt_str = [] prt_str.append("数据更新时间:"+data_json['lastUpdateTime']) prt_str.append("全国" + ":" + "累计确诊病例:" + str(data_json['chinaTotal']['confirm']) + \ "累计疑似病例:" + str(data_json['chinaTotal']['suspect']) + \ "累计死亡病例:" + str(data_json['chinaTotal']['dead']) + \ "累计出院病例:" + str(data_json['chinaTotal']['heal']) + \ "今日新增确诊病例:" + str(data_json['chinaAdd']['confirm']) + \ "今日新增疑似病例:" + str(data_json['chinaAdd']['suspect']) + \ "今日新增死亡病例:" + str(data_json['chinaAdd']['dead']) + \ "今日新增出院病例:" + str(data_json['chinaAdd']['heal'])) for province_list in data_tree_dict: for provice_name in self.province: if provice_name in province_list['name']: city_list = province_list['children'] prt_str.append(province_list['name'] + ":" + "累计确诊病例:" + str(province_list['total']['confirm']) + \ "累计死亡病例:" + str(province_list['total']['dead']) + \ "累计出院病例:" + str(province_list['total']['heal']) + \ "今日新增确诊病例:" + str(province_list['today']['confirm']) + \ "今日新增死亡病例:" + str(province_list['today']['dead']) + \ "今日新增出院病例:" + str(province_list['today']['heal'])) if provice_name == '山东': for data_dict in city_list: prt_str.append(data_dict['name'] + ":" + "累计确诊病例:" + str(data_dict['total']['confirm']) + \ "累计死亡病例:" + str(data_dict['total']['dead']) + \ "累计出院病例:" + str(data_dict['total']['heal']) + \ "今日确诊病例:" + str(data_dict['today']['confirm']) + \ "今日死亡病例:" + str(data_dict['today']['dead']) + \ "今日出院病例:" + str(data_dict['today']['heal'])) for item in prt_str: print(item) a = data_tree_dict # print(type(data_tree_dict['chinaTotal'])) # print(data_tree_dict.keys()) def write_page(self): pass def show(self): pass def show(self): self.down_page() self.parse_page() if __name__ == '__main__': url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h6' header = { 'user - agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' } province = ['湖北','山东'] wf = epidemic_data(province) wf.show()
感谢各位的阅读!关于“python如何爬取疫情数据”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!