|
|
@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
import pymysql
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
#爬取数据
|
|
|
|
|
|
|
|
# 国内疫情数据概况
|
|
|
|
|
|
|
|
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'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 国内疫情数据实例化
|
|
|
|
|
|
|
|
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'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################################################
|
|
|
|
|
|
|
|
#存储数据于数据库
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#保存国内疫情概况数据
|
|
|
|
|
|
|
|
# def save_home_realtime_datas(self,insideSummary):
|
|
|
|
|
|
|
|
# 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)'
|
|
|
|
|
|
|
|
# res=self.get_inside_summary_tuple(insideSummary)
|
|
|
|
|
|
|
|
# try:
|
|
|
|
|
|
|
|
# self.cursor.execut(sql,res)
|
|
|
|
|
|
|
|
# except Exception as e:
|
|
|
|
|
|
|
|
# print(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def connect():
|
|
|
|
|
|
|
|
# 请在这里补充代码,完成本关任务,注意缩进格式为4个空格
|
|
|
|
|
|
|
|
# ********* Begin ********* #
|
|
|
|
|
|
|
|
conn=pymysql.connect( # mysql服务器主机地址
|
|
|
|
|
|
|
|
host='localhost',
|
|
|
|
|
|
|
|
# 用户名
|
|
|
|
|
|
|
|
user='root',
|
|
|
|
|
|
|
|
# 数据库名
|
|
|
|
|
|
|
|
db='covid19_datas_guangxi',
|
|
|
|
|
|
|
|
# 用户密码
|
|
|
|
|
|
|
|
passwd='kfq991122',
|
|
|
|
|
|
|
|
# 编码格式
|
|
|
|
|
|
|
|
charset='utf8')
|
|
|
|
|
|
|
|
return conn
|
|
|
|
|
|
|
|
# ********* End ********* #
|
|
|
|
|
|
|
|
def insert(res):
|
|
|
|
|
|
|
|
# 创建连接,并且返回连接对象
|
|
|
|
|
|
|
|
conn = connect()
|
|
|
|
|
|
|
|
# 创建游标对象
|
|
|
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
|
|
|
# ********* Begin ********* #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ********* End ********* #
|
|
|
|
|
|
|
|
# 关闭游标
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
# 关闭连接
|
|
|
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
insert(insideSummary.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)
|