diff --git a/Di.py b/Di.py index 085dbd3..58b5c7a 100644 --- a/Di.py +++ b/Di.py @@ -1,9 +1,10 @@ import requests import re -import time +import datetime from bs4 import BeautifulSoup import lxml import json +import pymysql class summary_cn: def __init__(self): @@ -27,9 +28,33 @@ class summary_cn: self.curConfirm = 0 self.curConfirmRelative = 0 self.icuDisable = 0 - self.mapLastUpdatedTime=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 = '截止' + 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) @@ -67,28 +92,27 @@ def getCnSummary(): sum.curConfirm = int(ans['curConfirm']) sum.curConfirmRelative = int(ans['curConfirmRelative']) sum.icuDisable = int(ans['icuDisable']) - sum.mapLastUpdatedTime=tagjs['component'][0]['mapLastUpdatedTime'] - print(sum) + sum.mapLastUpdatedTime = tagjs['component'][0]['mapLastUpdatedTime'] + #print(sum) return sum def getCnNews(): - from selenium import webdriver - b=webdriver.Chrome() + b = webdriver.Chrome() b.get("https://voice.baidu.com/act/newpneumonia/newpneumonia") - txt=b.page_source + 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=[] + 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') + tmp = str(i.string) + tmp+=' ' + j.get('href') ans.append(tmp) print(tmp) return ans if __name__ == "__main__": - #undefined_D() - #getCnNews() + getCnSummary() +