You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
p9ew5o3q7/insideSummary.py

125 lines
4.7 KiB

import requests
import re
4 years ago
import datetime
from bs4 import BeautifulSoup
import lxml
import json
4 years ago
import pymysql
import time
class summary_cn:
def __init__(self):
self.confirmed = 0
self.died = 0
self.cured = 0
self.asymptomatic = 0
self.asymptomaticRelative = 0
self.unconfirmed = 0
self.relativeTime = 0
self.confirmedRelative = 0
self.unconfirmedRelative = 0
self.curedRelative = 0
self.diedRelative = 0
self.icu = 0
self.icuRelative = 0
self.overseasInput = 0
self.unOverseasInputCumulative = 0
self.overseasInputRelative = 0
self.unOverseasInputNewAdd = 0
self.curConfirm = 0
self.curConfirmRelative = 0
self.icuDisable = 0
4 years ago
self.mapLastUpdatedTime = 0
def get_db_tuple(self):
return (self.confirmed,self.died,self.cured,self.asymptomatic,self.asymptomaticRelative,
self.unconfirmed,self.relativeTime,self.confirmedRelative,self.unconfirmedRelative,
self.curedRelative,self.diedRelative,self.icu,self.icuRelative,self.overseasInput,
self.unOverseasInputCumulative,self.overseasInputRelative,self.unOverseasInputNewAdd,
self.curConfirm,self.curConfirmRelative,self.icuDisable,self.mapLastUpdatedTime,
datetime.datetime.now().strftime('%Y-%m-%d'))
def delete_sql(self,con):
sql="""
delete from insideSummary
"""
try:
cursor=con.cursor()
cursor.execute(sql)
except Exception as e:
print(e)
print('delete inside summary failed.')
else:
print('delete inside summary successfully')
def insert_sql(self,con):
self.delete_sql(con)
4 years ago
sql='''
insert into insideSummary(
confirmed,died,cured,asymptomatic,asymptomaticRelative,
unconfirmed,relativeTime,confirmedRelative,unconfirmedRelative,
curedRelative,diedRelative,icu,icuRelative,overseasInput,
unOverseasInputCumulative,overseasInputRelative,unOverseasInputNewAdd,
curConfirm,curConfirmRelative,icuDisable,mapLastUpdatedTime,InsertTime)
values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
4 years ago
'''
try:
con.cursor().execute(sql,self.get_db_tuple())
con.commit()
except Exception as e:
print(e)
print('Insert insideSummary failed.')
4 years ago
else:
print('Inserted insideSummary successfully')
def __str__(self):
4 years ago
string = '截止' + self.mapLastUpdatedTime + '\n'
string+= "国内现有\n\t确诊:" + str(self.curConfirm) + '\t较昨日' + str(self.curConfirmRelative)
string+="\n\t无症状:" + str(self.asymptomatic) + '\t较昨日' + str(self.asymptomaticRelative)
string+='\n\t重症:' + str(self.icu) + '\t较昨日' + str(self.icuRelative)
string+='\n\t疑似:' + str(self.unconfirmed) + '\t\t较昨日' + str(self.unconfirmedRelative)
string+='\n累计:\n\t确诊:' + str(self.confirmed) + '\n\t治愈:' + str(self.cured)
string+='\n\t死亡' + str(self.died)
return string
def getCnSummary():
res = requests.get("https://voice.baidu.com/act/newpneumonia/newpneumonia/")
txt = res.content.decode("utf-8")
soup = BeautifulSoup(txt,'html.parser')
tag = soup.find('script',attrs={'id':'captain-config'})
tagjs = json.loads(tag.string)
ans = tagjs['component'][0]['summaryDataIn']
sum = summary_cn()
sum.confirmed = int(ans['confirmed'])
sum.died = int(ans['died'])
sum.cured = int(ans['cured'])
sum.asymptomatic = int(ans['asymptomatic'])
sum.asymptomaticRelative = int(ans['asymptomaticRelative'])
sum.unconfirmed = int(ans['unconfirmed'])
sum.relativeTime = int(ans['relativeTime'])
sum.confirmedRelative = int(ans['confirmedRelative'])
sum.unconfirmedRelative = int(ans['unconfirmedRelative'])
sum.curedRelative = int(ans['curedRelative'])
sum.diedRelative = int(ans['diedRelative'])
sum.icu = int(ans['icu'])
sum.icuRelative = int(ans['icuRelative'])
sum.overseasInput = int(ans['overseasInput'])
sum.unOverseasInputCumulative = int(ans['unOverseasInputCumulative'])
sum.overseasInputRelative = int(ans['overseasInputRelative'])
sum.unOverseasInputNewAdd = int(ans['unOverseasInputNewAdd'])
sum.curConfirm = int(ans['curConfirm'])
sum.curConfirmRelative = int(ans['curConfirmRelative'])
sum.icuDisable = int(ans['icuDisable'])
###
4 years ago
sum.mapLastUpdatedTime = tagjs['component'][0]['mapLastUpdatedTime']
#print(sum)
return sum
def printStart():
print(getCnSummary())
4 years ago
def mysqlStart(con):
sum=getCnSummary()
sum.insert_sql(con)