parent
9c4b610d54
commit
3c2d820389
@ -0,0 +1,103 @@
|
||||
class Outside:
|
||||
def __init__(self):
|
||||
self.confirmed = 0
|
||||
self.died = 0
|
||||
self.crued = 0
|
||||
self.relativeTime = 0
|
||||
self.confirmedRelative = 0
|
||||
#self.asymptomatic = 0
|
||||
self.curConfirm = 0
|
||||
self.icuDisable = 0
|
||||
self.area = ''
|
||||
self.subList = []
|
||||
def __str__(self):
|
||||
return '地区: %s\t累计确诊:%s \t死亡: %d \t治愈: %d \t时间: %s \t新增: %s\t 现有: %d' % (self.area, self.confirmed, self.died, self.crued, self.relativeTime, self.confirmedRelative, self.curConfirm, )
|
||||
|
||||
class City:
|
||||
def __init__(self):
|
||||
self.confirmed = 0
|
||||
self.died = 0
|
||||
self.crued = 0
|
||||
self.curConfirm = 0
|
||||
self.city = ''
|
||||
|
||||
def __str__(self):
|
||||
return '城市:%s\t累计确诊:%d\t死亡:%d\t治愈:%d\t现有:%d' % (self.city,self.confirmed,self.died,self.crued,self.curConfirm)
|
||||
|
||||
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import re
|
||||
import json
|
||||
|
||||
def getOrElse(target, key):
|
||||
ret = ''
|
||||
if target.get(key) != None:
|
||||
ret = target[key]
|
||||
else:
|
||||
ret = ''
|
||||
return ret
|
||||
|
||||
def processStr(s):
|
||||
ret = []
|
||||
if s==None or s == '':
|
||||
ret = "0"
|
||||
else:
|
||||
ret = s
|
||||
return ret
|
||||
# 打印信息
|
||||
def print_all_infos(infos):
|
||||
for p in infos:
|
||||
print(p)
|
||||
for c in p.subList:
|
||||
print(c)
|
||||
print('+++++++++++++++++++++++++++++++++' * 3)
|
||||
#爬取页面
|
||||
res=requests.get('https://voice.baidu.com/act/newpneumonia/newpneumonia')
|
||||
|
||||
|
||||
# res = res.content.decode('utf-8')
|
||||
res=res.text
|
||||
soup = BeautifulSoup(res,'html.parser')
|
||||
|
||||
#print(soup)
|
||||
|
||||
tag = soup.find('script',attrs={'id':'captain-config'})
|
||||
|
||||
tagstr=tag.string
|
||||
|
||||
tagJson=json.loads(tagstr)
|
||||
|
||||
alleDataInfos = tagJson['component']
|
||||
|
||||
# results = re.findall('(\{"died".*?"subList".*?\})',tagstr)
|
||||
# print(len(results))
|
||||
# print(results)
|
||||
outsideDataInfos = alleDataInfos[0]['caseOutsideList']
|
||||
all_outside = []
|
||||
|
||||
for item in outsideDataInfos:
|
||||
outside = Outside()
|
||||
outside.confirmed = int(item['confirmed'])
|
||||
outside.died = int(item['died'])
|
||||
outside.crued = int(item['crued'])
|
||||
outside.relativeTime = time.strftime("%Y-%m-%d ", time.localtime( int(item['relativeTime'])))
|
||||
outside.confirmedRelative = int(item['confirmedRelative'])
|
||||
#self.asymptomatic = 0
|
||||
outside.curConfirm = int(item['curConfirm'])
|
||||
outside.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.curConfirm = int(processStr(getOrElse(cityitem, 'curConfirm')))
|
||||
outside.subList.append(city)
|
||||
|
||||
all_outside.append(outside)
|
||||
|
||||
print_all_infos(all_outside)
|
Loading…
Reference in new issue