@ -2,6 +2,7 @@ import re
import json
import requests
import pymysql
import datetime
##################################################################################################
#爬取数据
# 国内疫情数据概况
@ -58,59 +59,47 @@ for key in dict:
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 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 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 ( )
conn = pymysql . connect ( host = ' localhost ' , user = ' root ' , db = ' covid19_datas_guangxi ' , passwd = ' kfq991122 ' , charset = ' utf8 ' )
# 创建游标对象
cursor = conn . cursor ( )
# ********* Begin ********* #
date = data [ ' lastUpdateTime ' ]
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 :
@ -119,13 +108,11 @@ def insert(res):
except Exception as e :
print ( e )
print ( ' +++ save_province_datas is over. ' )
# ********* End ********* #
# 关闭游标
cursor . close ( )
# 关闭连接
conn . close ( )
insert ( insideSummary . get_inside_summary_tuple ( ) )
insert ( fetch_inside_summary ( data ) . get_inside_summary_tuple ( ) )
conn = pymysql . connect ( ' localhost ' , ' root ' , ' kfq991122 ' , ' covid19_datas_guangxi ' )