def get_all_new_data(self, datall): for i in datall: data = i['date'] # 时间 city = i['city'] # 城市 confirm = i['confirm'] # 确证人数 dead = i['dead'] # 死亡人数 heal = i['heal'] # 治愈人数 confadd = i['confirm_add'] # 新增人数 if confadd is '': confadd = 0 sql = 'insert into feiyanpd(data,country,confirm,dead,heal,confirm_add,city) values ({},"{}",{},{},{},{},"{}")'.format( data, self.province, confirm, dead, heal, confadd, city) ws().do_insert_data(sql)
def insert_data(self): # 获取网络上的最新数据,去掉前面的0 datall = self.get_data() # 从数据库中读取数据,确认是否存在最新时间的数据 sql = 'select data,confirm_add from feiyanpd WHERE city="{}" ORDER BY data desc LIMIT 1'.format( self.city) db_data = ws().do_sql_one(sql) # 如果出现异常,那就是说没有相关数据,执行增加操作 try: # 数据库时间 db_date = db_data['data'] # 数据库新增人数 db_priadd = db_data['confirm_add'] # 网络时间 da = datall[-1]['date'].lstrip('0') # 网络上新增人数 daadd = datall[-1]['confirm_add'] if daadd is '': daadd = 0 # 判断时间是否相等 if db_date == da: # 判断新增人数是否一致 if int(db_priadd) == int(daadd): print('数据一致') else: print('修改数据') # for i in datall: # data, city, confirm, dead, heal, confadd = self.get_onerow_data(i) # # 先清空数据库 # print('清空数据库') # self.delete_data(city) # 先清空数据库 print('清空数据库') self.delete_data(self.city) # 再添加数据 print('添加数据库') self.get_all_new_data(datall) #data, city, confirm, dead, heal, confadd = self.get_onerow_data(datall[-1]) #self.updata_data(confirm,dead,heal,confadd,data,self.province,city) # 时间不相等,那就全部重新更新 else: # for i in datall: # data, city, confirm, dead, heal, confadd = self.get_onerow_data(i) # # 先清空数据库 # print('清空数据库') # self.delete_data(city) # 先清空数据库 print('清空数据库') self.delete_data(self.city) # 再添加数据 print('添加数据库') self.get_all_new_data(datall) except TypeError as e: # 执行新增操作 self.get_all_new_data(datall)
def __init__(self, province, city): self.city = city self.province = province # 确保数据是最新的 GetDate(self.province, self.city).insert_data() # 从数据库中获取数据并画图 self.sql = 'SELECT * FROM feiyanpd where country="{}" and city="{}"'.format( self.province, self.city) self.dataall = ws().do_sql_all(self.sql) # 获取时间 self.date = jsonpath.jsonpath(self.dataall, "$..data") # 确诊人数 self.confirm = jsonpath.jsonpath(self.dataall, '$..confirm') # 死亡人数 self.dead = jsonpath.jsonpath(self.dataall, '$..dead') # 治愈人数 self.heal = jsonpath.jsonpath(self.dataall, '$..heal') # 新增人数 self.confirm_add = jsonpath.jsonpath(self.dataall, '$..confirm_add') # 画图 self.get_pic()
def __init__(self): # 因为页面修改,无法重页面获取span中的值,所以直接读数据库 # self.wb = webdriver.Chrome() # self.wb.get('https://news.qq.com/zt2020/page/feiyan.htm') # time.sleep(1) # area = self.wb.find_elements_by_xpath( # #'//div[@class="placeItemWrap"]/div[@class="clearfix placeItem placeArea"]/h2' # # 2.25修改 # '//div[@id="listWraper"]/table[2]/tbody/tr[@class="areaBox"]/th/span' # ) # for i in range(1, len(area) + 1): # if i == 0: # privince = '湖北' # else: # privince = self.wb.find_element_by_xpath( # # '//div[@class="placeItemWrap"][{}]/div[@class="clearfix placeItem placeArea"]/h2'.format(i) # '//div[@id="listWraper"]/table[2]/tbody[{}]/tr[@class="areaBox"]/th/span'.format(i) # ).text # a = self.wb.find_elements_by_xpath( # #'//div[@area="{}"]/div'.format(privince) # '//div[@id="listWraper"]/table[2]/tbody[{}]/tr[@class="city"]/th/span'.format(i) # ) # for j in range(1, len(a) + 1): # #city = self.wb.find_element_by_xpath('//div[@area="{}"]/div[{}]'.format(privince, j)).get_attribute('city') # city = self.wb.find_element_by_xpath( # '//div[@id="listWraper"]/table[2]/tbody[{}]/tr[@class="city"][{}]/th[@class="area"]/span'.format(i, j)) # b = city.text # print(privince, b) # # 获取到省份和城市,进行全部数据的获取 # #GetDate(privince,city).insert_data() # self.wb.quit() sql = 'select t.country,t.city FROM (select country,city,COUNT(1) from feiyanpd GROUP BY country,city) as t' a = ws().do_sql_all(sql) for i in a: privince=i.get('country') city = i.get('city') print(privince, city) GetDate(privince,city).insert_data()
def delete_data(self, city): sql_delete = 'DELETE from feiyanpd WHERE city="{}"'.format(city) ws().do_updata(sql_delete)
def updata_data(self, confirm, dead, heal, confadd, data, country, city): sql_updat = 'UPDATE feiyanpd set ' \ 'confirm={},dead={},heal={},confirm_add={} ' \ 'where data={} and country="{}" and city="{}"'.format(confirm, dead, heal, confadd, data,country,city) ws().do_updata(sql_updat)