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.

119 lines
4.7 KiB

import requests
import re
import datetime
from bs4 import BeautifulSoup
import lxml
import json
import pymysql
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
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 insert_sql(con):
sql='''
insert into cn_sunmary(confirmed,died,cured,asymptomatic,asymptomaticRelative,
unconfirmed,relativeTime,confirmedRelative,unconfirmedRelative,
curedRelative,diedRelative,icu,icuRelative,overseasInput,
unOverseasInputCumulative,overseasInputRelative,unOverseasInputNewAdd,
curConfirm,curConfirmRelative,icuDisable,mapLastUpdatedTime,InsertTime) values(%d,%d,%d,
%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%s)
'''
try:
con.cursor().execute(sql,self.get_db_tuple())
con.commit()
except Exception as e:
print(e)
print('Insert failed.')
else:
print('Inserted successfully')
def __str__(self):
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'])
sum.mapLastUpdatedTime = tagjs['component'][0]['mapLastUpdatedTime']
#print(sum)
return sum
def getCnNews():
b = webdriver.Chrome()
b.get("https://voice.baidu.com/act/newpneumonia/newpneumonia")
txt = b.page_source
b.quit()
soup = BeautifulSoup(txt,'html.parser')
tag1 = soup.select('div[class="Virus_1-1-284_2CVyXP"]')
tag2 = soup.select('div[class="Virus_1-1-284_TB6x3k"] > a[href]')
ans = []
for i,j in zip(tag1,tag2):
tmp = str(i.string)
tmp+=' ' + j.get('href')
ans.append(tmp)
print(tmp)
return ans
if __name__ == "__main__":
getCnSummary()