import pymysql import requests from bs4 import BeautifulSoup import json import re import pymysql def getData(DB): cs = DB.cursor() cs.execute('select * from cn') res = cs.fetchone() print(res) def delData(DB,day): cs = DB.cursor() tmp = day[0:11:1] +'%' sql = 'delete from cn where date like %s' data = [tmp] try: cs.executemany(sql,data) DB.commit() except Exception as e: print('删除不成功') print(e) def fun1(summary): tmp = summary.__dict__ d = [] for item in tmp: d.append(tmp[item]) return d def save_summary(host,user, password, database): #db = pymysql.connect('localhost', 'root', '123456', 'covid19') db = pymysql.connect(host,user, password, database) cs = db.cursor() tmp,time = Mod_Summary_CN() D = fun1(tmp) tmp1 = [] for i in range(0,18): tmp1.append(D[i]) tmp1.append(time) tmp2 = tuple(tmp1) sql = 'insert into cn(confirmed, confirmed_R, curConfirm, curConfirm_R, died, died_R,cured, cured_R, asymptomatic, asymptomatic_R, unconfirmed, \ unconfirmed_R, icu, icu_R, overseasIn, overseasIn_R, unOverseasInC, unOverseasInA, date)values(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' data = [] data.append(tmp2) try: delData(db,time) cs.executemany(sql,data) except Exception as e: print('添加不成功') print(e) else: db.commit() print('添加成功') getData(db) cs.close() db.close()