這篇文章給大家分享的是有關python如何爬取疫情數(shù)據(jù)的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
為郊區(qū)等地區(qū)用戶提供了全套網頁設計制作服務,及郊區(qū)網站建設行業(yè)解決方案。主營業(yè)務為成都網站設計、成都網站制作、郊區(qū)網站設計,以傳統(tǒng)方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!疫情數(shù)據(jù)
程序源碼
// 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'])) # 因為解析數(shù)據(jù)為 data 前有一個"",所以數(shù)據(jù)類型為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("數(shù)據(jù)更新時間:"+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如何爬取疫情數(shù)據(jù)”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!