python 爬取疫情数据的源码
疫情数据
程序源码
//Anhighlightedblock importrequests importjson classepidemic_data(): def__init__(self,province): self.url=url self.header=header self.text={} self.province=province #self.r=None defdown_page(self): r=requests.get(url=url,headers=header) self.text=r.text #self.r=r defparse_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'])) forprovince_listindata_tree_dict: forprovice_nameinself.province: ifprovice_nameinprovince_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'])) ifprovice_name=='山东': fordata_dictincity_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'])) foriteminprt_str: print(item) a=data_tree_dict #print(type(data_tree_dict['chinaTotal'])) #print(data_tree_dict.keys()) defwrite_page(self): pass defshow(self): pass defshow(self): self.down_page() self.parse_page() if__name__=='__main__': url='https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5' header={ 'user-agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/79.0.3945.130Safari/537.36' } province=['湖北','山东'] wf=epidemic_data(province) wf.show()
总结
以上所述是小编给大家介绍的python爬取疫情数据的源码,希望对大家有所帮助!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。