From e6f44ca00cd74c2228686314e6df16a0104542a1 Mon Sep 17 00:00:00 2001 From: Your Namehzh <494809973@qq.com> Date: Fri, 14 Aug 2020 15:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BD=E5=86=85=E7=96=AB=E6=83=85=E5=AE=9E?= =?UTF-8?q?=E6=97=B6=E6=A6=82=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.txt | 1 - Main.txt | 79 +++++++++++++++++++++++++++++++++++++++ Mod_Summary_CN().txt | 50 +++++++++++++++++++++++++ class Summary.txt | 30 +++++++++++++++ 各字段对应属性.txt | 31 +++++++++++++++ 5 files changed, 190 insertions(+), 1 deletion(-) delete mode 100644 1.txt create mode 100644 Main.txt create mode 100644 Mod_Summary_CN().txt create mode 100644 class Summary.txt create mode 100644 各字段对应属性.txt diff --git a/1.txt b/1.txt deleted file mode 100644 index 9d07aa0..0000000 --- a/1.txt +++ /dev/null @@ -1 +0,0 @@ -111 \ No newline at end of file diff --git a/Main.txt b/Main.txt new file mode 100644 index 0000000..4b658f1 --- /dev/null +++ b/Main.txt @@ -0,0 +1,79 @@ +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() + + diff --git a/Mod_Summary_CN().txt b/Mod_Summary_CN().txt new file mode 100644 index 0000000..682a5ae --- /dev/null +++ b/Mod_Summary_CN().txt @@ -0,0 +1,50 @@ +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) + diff --git a/class Summary.txt b/class Summary.txt new file mode 100644 index 0000000..88aef4a --- /dev/null +++ b/class Summary.txt @@ -0,0 +1,30 @@ +class Summary: + import time + + def __init__(self): + self.confirmed = 0 + self.confirmedRelative = 0 + self.curConfirm = 0 + self.curConfirmRelative = 0 + self.died = 0 + self.diedRelative = 0 + self.cured = 0 + self.curedRelative = 0 + self.asymptomatic = 0 + self.asymptomaticRelative = 0 + self.unconfirmed = 0 + self.unconfirmedRelative = 0 + self.icu = 0 + self.icuRelative = 0 + self.overseasInput = 0 + self.overseasInputRelative = 0 + self.unOverseasInputCumulative = 0 + self.unOverseasInputNewAdd = 0 + self.icuDisable = 0 + + + def __str__(self): + return '\n累计确诊:%d\n累计确诊增加:%d\n现有确诊:%d\n新增确诊(较昨日):%d\n累计死亡:%d\n新增死亡:%d\n累计治愈:%d\n新增治愈:%d\n\ +累计无症状感染者:%d\n新增无症状感染者:%d\n现有疑似:%d\n新增疑似:%d\n现有重症:%d\n新增重症:%d\n累计境外输入:%d\n\ +新增境外输入:%d\n累计非境外输入的确诊:%d\n非境外输入的新增确诊:%d\n???:%d'\ + %(self.confirmed,self.confirmedRelative,self.curConfirm,self.curConfirmRelative,self.died,self.diedRelative,self.cured,self.curedRelative,self.asymptomatic,self.asymptomaticRelative,self.unconfirmed,self.unconfirmedRelative,self.icu,self.icuRelative,self.overseasInput,self.overseasInputRelative,self.unOverseasInputCumulative,self.unOverseasInputNewAdd,self.icuDisable) \ No newline at end of file diff --git a/各字段对应属性.txt b/各字段对应属性.txt new file mode 100644 index 0000000..f8f6ccc --- /dev/null +++ b/各字段对应属性.txt @@ -0,0 +1,31 @@ +【summaryDataIn】 + +"confirmed": 累计确诊:%d\n +"confirmedRelative": 累计确诊增加:%d\n + +"curConfirm": 现有确诊:%d\n +"curConfirmRelative": 新增确诊(较昨日):%d\n + +"died": 累计死亡:%d\n +"diedRelative": 新增死亡:%d\n + +"cured": 累计治愈:%d\n +"curedRelative": 新增治愈:%d\n + +"asymptomatic": 累计无症状感染者:%d\n +"asymptomaticRelative": 新增无症状感染者:%d\n + +"unconfirmed": 现有疑似:%d\n +"unconfirmedRelative": 新增疑似:%d\n + +"icu": 现有重症:%d\n +"icuRelative": 新增重症:%d\n + +"overseasInput": 累计境外输入:%d\n +"overseasInputRelative": 新增境外输入:%d\n + +"unOverseasInputCumulative": 累计非境外输入的确诊:%d\n +"unOverseasInputNewAdd": 非境外输入的新增确诊:%d\n + +"relativeTime": 时间:%d\n +"icuDisable": ???