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.
125 lines
5.3 KiB
125 lines
5.3 KiB
4 years ago
|
import re
|
||
|
import json
|
||
|
import requests
|
||
|
import pymysql
|
||
|
import datetime
|
||
|
##################################################################################################
|
||
|
#爬取数据
|
||
|
# 国内疫情数据概况
|
||
|
class InsideSummary:
|
||
|
def __init__(self):
|
||
|
self.curConfirm = 0 #现有确诊
|
||
|
self.curConfirmRelative = 0#较昨日新增确诊
|
||
|
self.asymptomatic = 0 #无症状感染
|
||
|
self.asymptomaticRelative = 0 #较昨日新增无症状感染
|
||
|
self.unconfirmed = 0 #现有疑似
|
||
|
self.unconfirmedRelative = 0#较昨日疑似新增
|
||
|
self.icu = 0 #现有重症
|
||
|
self.icuRelative = 0 #较昨日重症病例新增
|
||
|
self.confirmed = 0 #累计确诊
|
||
|
self.confirmedRelative = 0 #较昨日累计确诊新增
|
||
|
self.overseasInput = 0 #累计境外输入
|
||
|
self.overseasInputRelative = 0#较昨日累计境外输入新 增
|
||
|
self.cured = 0 #累计治愈
|
||
|
self.curedRelative = 0 #较昨日累计治愈新增
|
||
|
self.died = 0 #累计死亡
|
||
|
self.diedRelative = 0#较昨日累计死亡新增
|
||
|
self.updatedTime = 0 #发布时间
|
||
|
# 数据库接口
|
||
|
def get_inside_summary_tuple(self):
|
||
|
return ((self.curConfirm, self.curConfirmRelative, self.asymptomatic, self.asymptomaticRelative, \
|
||
|
self.unconfirmed, self.unconfirmedRelative, self.icu, self.icuRelative, self.confirmed, \
|
||
|
self.confirmedRelative, self.overseasInput, self.overseasInputRelative, self.cured, self.curedRelative, \
|
||
|
self.died, self.diedRelative, self.updatedTime))
|
||
|
# 输出接口
|
||
|
def __str__(self):
|
||
|
return '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s' % (
|
||
|
self.curConfirm, self.curConfirmRelative, self.asymptomatic, self.asymptomaticRelative, self.unconfirmed,
|
||
|
self.unconfirmedRelative, self.icu, self.icuRelative, self.confirmed, self.confirmedRelative,
|
||
|
self.overseasInput, self.overseasInputRelative, self.cured, self.curedRelative, self.died, self.diedRelative,
|
||
|
self.updatedTime)
|
||
|
#######################################################################################################
|
||
|
#业务逻辑类
|
||
|
|
||
|
# 爬取国内疫情数据
|
||
|
res=requests.get('https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5')
|
||
|
#解码
|
||
|
res=res.content.decode('utf-8')
|
||
|
|
||
|
dict=json.loads(res) #str->dict
|
||
|
|
||
|
#规范数据
|
||
|
for key in dict:
|
||
|
try:
|
||
|
dict[key] = dict[key].replace('\\', '')
|
||
|
key=key.replace('\\','')
|
||
|
except:
|
||
|
pass
|
||
|
#新生成data字典
|
||
|
data=json.loads(dict['data'])
|
||
|
|
||
|
# 国内疫情数据实例化
|
||
|
# # #赋值
|
||
|
def fetch_inside_summary(data):
|
||
|
insideSummary = InsideSummary()
|
||
|
insideSummary.curConfirm = int(data['chinaTotal']['nowConfirm'])
|
||
|
insideSummary.curConfirmRelative = int(data['chinaAdd']['nowConfirm'])
|
||
|
insideSummary.asymptomatic = int(data['chinaTotal']['noInfect'])
|
||
|
insideSummary.asymptomaticRelative = int(data['chinaAdd']['noInfect'])
|
||
|
insideSummary.unconfirmed = int(data['chinaTotal']['suspect'])
|
||
|
insideSummary.unconfirmedRelative = int(data['chinaAdd']['suspect'])
|
||
|
insideSummary.icu = int(data['chinaTotal']['nowSevere'])
|
||
|
insideSummary.icuRelative = int(data['chinaAdd']['nowSevere'])
|
||
|
insideSummary.confirmed = int(data['chinaTotal']['confirm'])
|
||
|
insideSummary.updatedTime = data['lastUpdateTime']
|
||
|
insideSummary.confirmedRelative = int(data['chinaAdd']['confirm'])
|
||
|
insideSummary.overseasInput = int(data['chinaTotal']['importedCase'])
|
||
|
insideSummary.overseasInputRelative = int(data['chinaAdd']['importedCase'])
|
||
|
insideSummary.cured = int(data['chinaTotal']['heal'])
|
||
|
insideSummary.curedRelative = int(data['chinaAdd']['heal'])
|
||
|
insideSummary.died = int(data['chinaTotal']['dead'])
|
||
|
insideSummary.diedRelative = int(data['chinaAdd']['dead'])
|
||
|
return insideSummary
|
||
|
##########################################################################################################
|
||
|
#存储数据于数据库
|
||
|
|
||
|
#保存国内疫情概况数据
|
||
|
|
||
|
def insert(res):
|
||
|
|
||
|
# 创建连接,并且返回连接对象
|
||
|
conn =pymysql.connect(host='localhost',user='root',db='covid19_datas_guangxi',passwd='kfq991122',charset='utf8')
|
||
|
# 创建游标对象
|
||
|
cursor = conn.cursor()
|
||
|
|
||
|
date = data['lastUpdateTime'].split(' ',1)[0]
|
||
|
print(date)
|
||
|
sql = 'delete from home_realtime_datas where updatedTime like "%s"'%(date+'%')
|
||
|
print('The update was successful')
|
||
|
try:
|
||
|
cursor.execute(sql)
|
||
|
conn.commit()
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
|
||
|
sql='insert into home_realtime_datas(curConfirm,curConfirmRelative,asymptomatic,asymptomaticRelative,unconfirmed,unconfirmedRelative,icu,icuRelative,confirmed,confirmedRelative,overseasInput,overseasInputRelative,cured,curedRelative,died,diedRelative,updatedTime) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
|
||
|
try:
|
||
|
cursor.execute(sql, res)
|
||
|
conn.commit()
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
print('+++ save_province_datas is over.')
|
||
|
cursor.close()
|
||
|
conn.close()
|
||
|
|
||
|
|
||
|
insert(fetch_inside_summary(data).get_inside_summary_tuple())
|
||
|
|
||
|
|
||
|
conn = pymysql.connect('localhost','root','kfq991122','covid19_datas_guangxi')
|
||
|
cursor = conn.cursor()
|
||
|
sql = 'select * from home_realtime_datas'
|
||
|
cursor.execute(sql)
|
||
|
results = cursor.fetchall()
|
||
|
print(results)
|