From 44ad4862aebb55a70fcff08673e1074feeb40d65 Mon Sep 17 00:00:00 2001 From: egg23333 <806261011@qq.com> Date: Wed, 12 Aug 2020 11:43:50 +0800 Subject: [PATCH] add getCnNews getCnSummary --- Di.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Di.py diff --git a/Di.py b/Di.py new file mode 100644 index 0000000..085dbd3 --- /dev/null +++ b/Di.py @@ -0,0 +1,94 @@ +import requests +import re +import time +from bs4 import BeautifulSoup +import lxml +import json + +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 __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(): + from selenium import webdriver + 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__": + #undefined_D() + #getCnNews()