class Province: def __init__(self): self.confirmed = 0# self.died = 0 self.crued = 0 self.confirmedRelative = 0 self.diedRelative = 0 self.curedRelative = 0 self.asymptomaticRelative = 0 self.asymptomatic = 0 self.curConfirm = 0 self.curConfirmRelative = 0 self.area = '' self.pub_date = '' self.subList = [] def __str__(self): return '%s\n省份: %s\n累计确诊: %s(较昨日: %+d)\n累计死亡: %d(较昨日: %+d)\n\ 累计治愈: %d(较昨日: %+d)\n现有确诊: %d(较昨日: %+d) \n无症状感染者: %d(较昨日: %+d)\n'% (self.pub_date,self.area, self.confirmed,self.confirmedRelative, self.died,self.diedRelative,self.crued, self.curedRelative, self.curConfirm,self.curConfirmRelative,self.asymptomatic,self.asymptomaticRelative ) class City(): def __init__(self): self.city = '' self.confirmed = 0 self.died = 0 self.crued = 0 # self.confirmedRelative = 0 self.curConfirm = 0 def __str__(self): return '%s\n累计确诊: %d\n累计死亡: %d\n累计治愈: \ %d\n现存确诊: %d'% (self.city,self.confirmed, self.died, self.crued,self.curConfirm) import requests from bs4 import BeautifulSoup import re import json def getOrElse(a,key): ret = '' if a.get(key) != None: ret = a[key] else: ret = '' return ret def processStr(s): ret = '' if s==None or s == '': ret = '0' else: ret = s return ret def Print(a): for p in a: print(p) if len(p.subList) != 0: print('- - - - - - - - - - - - - - - - -' * 4) print('省内各地级市:\n') else: pass for c in p.subList: print(c) print() print('---------------------------------' * 4) res = requests.get('https://voice.baidu.com/act/newpneumonia/newpneumonia/') res = res.text soup = BeautifulSoup(res,'html.parser') tag = soup.find('script',attrs={'id':'captain-config'}) tagstr = tag.string tagjson = json.loads(tagstr) dateinfos = tagjson['component'] insidedateinfos = dateinfos[0]['caseList'] pub_date = tagjson['component'][0]['mapLastUpdatedTime'] all_provinces = [] for item in insidedateinfos: province = Province() province.confirmed = int(processStr(getOrElse(item,'confirmed'))) province.died = int(processStr(getOrElse(item,'died'))) province.crued = int(processStr(getOrElse(item,'crued'))) province.confirmedRelative = int(processStr(getOrElse(item,'confirmedRelative'))) province.diedRelative = int(processStr(getOrElse(item,'diedRelative'))) province.curedRelative = int(processStr(getOrElse(item,'curedRelative'))) province.asymptomaticRelative = int(processStr(getOrElse(item,'asymptomaticRelative'))) province.asymptomatic = int(processStr(getOrElse(item,'asymptomatic'))) province.curConfirm = int(processStr(getOrElse(item,'curConfirm'))) province.curConfirmRelative = int(processStr(getOrElse(item,'curConfirmRelative'))) province.pub_date = pub_date province.area = item['area'] sublist = item['subList'] for cityitem in sublist: city = City() city.city = getOrElse(cityitem,'city') city.confirmed = int(processStr(getOrElse(cityitem,'confirmed'))) city.died = int(processStr(getOrElse(cityitem, 'died'))) city.crued = int(processStr(getOrElse(cityitem,'crued'))) city.confirmedRelative = int(processStr(getOrElse(cityitem, 'confirmedRelative'))) city.curConfirm = int(processStr(getOrElse(cityitem,'curConfirm'))) province.subList.append(city) all_provinces.append(province) Print(all_provinces)