zhousufan
parent
0fc7d1988b
commit
8d9abb0982
@ -1,79 +0,0 @@
|
||||
import pymysql
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import json
|
||||
import re
|
||||
|
||||
import pymysql
|
||||
def getData(DB):
|
||||
cs = DB.cursor()
|
||||
cs.execute('select * from cn')
|
||||
res = cs.fetchone()
|
||||
|
||||
print(res)
|
||||
|
||||
def delData(DB,day):
|
||||
cs = DB.cursor()
|
||||
tmp = day[0:11:1] +'%'
|
||||
sql = 'delete from cn where date like %s'
|
||||
data = [tmp]
|
||||
try:
|
||||
cs.executemany(sql,data)
|
||||
DB.commit()
|
||||
except Exception as e:
|
||||
print('删除不成功')
|
||||
print(e)
|
||||
|
||||
def fun1(summary):
|
||||
tmp = summary.__dict__
|
||||
|
||||
d = []
|
||||
|
||||
for item in tmp:
|
||||
d.append(tmp[item])
|
||||
|
||||
return d
|
||||
|
||||
def save_summary(host,user, password, database):
|
||||
|
||||
#db = pymysql.connect('localhost', 'root', '123456', 'covid19')
|
||||
db = pymysql.connect(host,user, password, database)
|
||||
|
||||
cs = db.cursor()
|
||||
|
||||
tmp,time = Mod_Summary_CN()
|
||||
D = fun1(tmp)
|
||||
|
||||
tmp1 = []
|
||||
|
||||
for i in range(0,18):
|
||||
tmp1.append(D[i])
|
||||
|
||||
tmp1.append(time)
|
||||
tmp2 = tuple(tmp1)
|
||||
|
||||
|
||||
sql = 'insert into cn(confirmed, confirmed_R, curConfirm, curConfirm_R, died, died_R,cured, cured_R, asymptomatic, asymptomatic_R, unconfirmed, \
|
||||
unconfirmed_R, icu, icu_R, overseasIn, overseasIn_R, unOverseasInC, unOverseasInA, date)values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
|
||||
|
||||
data = []
|
||||
data.append(tmp2)
|
||||
|
||||
|
||||
try:
|
||||
delData(db,time)
|
||||
cs.executemany(sql,data)
|
||||
except Exception as e:
|
||||
print('添加不成功')
|
||||
print(e)
|
||||
else:
|
||||
db.commit()
|
||||
print('添加成功')
|
||||
|
||||
|
||||
getData(db)
|
||||
|
||||
cs.close()
|
||||
db.close()
|
||||
|
||||
|
@ -1,50 +0,0 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import json
|
||||
import re
|
||||
|
||||
|
||||
def Mod_Summary_CN():
|
||||
response = requests.get('https://voice.baidu.com/act/newpneumonia/newpneumonia/')
|
||||
content = response.text
|
||||
soup = BeautifulSoup(content, 'html.parser')
|
||||
|
||||
|
||||
tag1 = soup.find('script', attrs={'id':'captain-config'})
|
||||
tagstr = str(tag1)
|
||||
res = re.findall(r'(\{".*?".*?\})',tagstr)
|
||||
|
||||
tagJson = json.loads(res[831])
|
||||
|
||||
temp1 = re.findall(r'("mapLastUpdatedTime":".*?")',tagstr)
|
||||
temp2 = str(temp1[0])
|
||||
time = temp2[22:len(temp2)-1:1]
|
||||
|
||||
|
||||
summary = Summary()
|
||||
summary.confirmed = int(tagJson['confirmed'])
|
||||
summary.confirmedRelative = int(tagJson['confirmedRelative'])
|
||||
summary.curConfirm = int(tagJson['curConfirm'])
|
||||
summary.curConfirmRelative = int(tagJson['curConfirmRelative'])
|
||||
summary.died = int(tagJson['died'])
|
||||
summary.diedRelative = int(tagJson['diedRelative'])
|
||||
summary.cured = int(tagJson['cured'])
|
||||
summary.curedRelative = int(tagJson['curedRelative'])
|
||||
summary.asymptomatic = int(tagJson['asymptomatic'])
|
||||
summary.asymptomaticRelative = int(tagJson['asymptomaticRelative'])
|
||||
summary.unconfirmed = int(tagJson['unconfirmed'])
|
||||
summary.unconfirmedRelative = int(tagJson['unconfirmedRelative'])
|
||||
summary.icu = int(tagJson['icu'])
|
||||
summary.icuRelative = int(tagJson['icuRelative'])
|
||||
summary.overseasInput = int(tagJson['overseasInput'])
|
||||
summary.overseasInputRelative = int(tagJson['overseasInputRelative'])
|
||||
summary.unOverseasInputCumulative = int(tagJson['unOverseasInputCumulative'])
|
||||
summary.unOverseasInputNewAdd = int(tagJson['unOverseasInputNewAdd'])
|
||||
summary.icuDisable = int(tagJson['icuDisable'])
|
||||
|
||||
|
||||
#print('---------------------------------------------------------')
|
||||
#print(' 更新时间:%s'%(time))
|
||||
#print('---------------------------------------------------------')
|
||||
#print(summary)
|
||||
|
@ -0,0 +1,120 @@
|
||||
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)
|
||||
|
Loading…
Reference in new issue