时间:2021-05-22
疫情数据
程序源码
// An highlighted blockimport requestsimport jsonclass 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_h5' 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 爬取疫情数据的源码,希望对大家有所帮助!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
思路:在腾讯疫情数据网站F12解析网站结构,使用Python爬取当日疫情数据和历史疫情数据,分别存储到details和history两个mysql表。①此方法用
本文实例讲述了Python实现爬虫爬取NBA数据功能。分享给大家供大家参考,具体如下:爬取的网站为:stat-nba.com,这里爬取的是NBA2016-201
在使用python爬取网站信息时,查看爬取完后的数据发现,数据并没有被爬取下来,这是因为网站中有UA这种请求载体的身份标识,如果不是基于某一款浏览器爬取则是不正
思路:使用Python爬虫对腾讯疫情网站世界疫情数据进行爬取,封装成一个函数返回一个 字典数据格式的对象,写另一个方法调用该函数接收返回值,和数据库取得连接后
本文实例讲述了Python利用Scrapy框架爬取豆瓣电影。分享给大家供大家参考,具体如下:1、概念Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应