add sql function

chenzw
egg23333 4 years ago
parent 44ad4862ae
commit 54d48cc949

52
Di.py

@ -1,9 +1,10 @@
import requests import requests
import re import re
import time import datetime
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import lxml import lxml
import json import json
import pymysql
class summary_cn: class summary_cn:
def __init__(self): def __init__(self):
@ -27,9 +28,33 @@ class summary_cn:
self.curConfirm = 0 self.curConfirm = 0
self.curConfirmRelative = 0 self.curConfirmRelative = 0
self.icuDisable = 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): 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.curConfirm) + '\t较昨日' + str(self.curConfirmRelative)
string+="\n\t无症状:" + str(self.asymptomatic) + '\t较昨日' + str(self.asymptomaticRelative) 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.icu) + '\t较昨日' + str(self.icuRelative)
@ -67,28 +92,27 @@ def getCnSummary():
sum.curConfirm = int(ans['curConfirm']) sum.curConfirm = int(ans['curConfirm'])
sum.curConfirmRelative = int(ans['curConfirmRelative']) sum.curConfirmRelative = int(ans['curConfirmRelative'])
sum.icuDisable = int(ans['icuDisable']) sum.icuDisable = int(ans['icuDisable'])
sum.mapLastUpdatedTime=tagjs['component'][0]['mapLastUpdatedTime'] sum.mapLastUpdatedTime = tagjs['component'][0]['mapLastUpdatedTime']
print(sum) #print(sum)
return sum return sum
def getCnNews(): def getCnNews():
from selenium import webdriver b = webdriver.Chrome()
b=webdriver.Chrome()
b.get("https://voice.baidu.com/act/newpneumonia/newpneumonia") b.get("https://voice.baidu.com/act/newpneumonia/newpneumonia")
txt=b.page_source txt = b.page_source
b.quit() b.quit()
soup = BeautifulSoup(txt,'html.parser') soup = BeautifulSoup(txt,'html.parser')
tag1 = soup.select('div[class="Virus_1-1-284_2CVyXP"]') tag1 = soup.select('div[class="Virus_1-1-284_2CVyXP"]')
tag2=soup.select('div[class="Virus_1-1-284_TB6x3k"] > a[href]') tag2 = soup.select('div[class="Virus_1-1-284_TB6x3k"] > a[href]')
ans=[] ans = []
for i,j in zip(tag1,tag2): for i,j in zip(tag1,tag2):
tmp=str(i.string) tmp = str(i.string)
tmp+=' '+j.get('href') tmp+=' ' + j.get('href')
ans.append(tmp) ans.append(tmp)
print(tmp) print(tmp)
return ans return ans
if __name__ == "__main__": if __name__ == "__main__":
#undefined_D() getCnSummary()
#getCnNews()

Loading…
Cancel
Save