import requests
import json
import os
class 疫情():
def __init__(self):
url='https://c.m.163.com/ug/api/wuhan/app/data/list-total'
headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.81 Safari/537.36 Edg/104.0.1293.54'}
s=requests.session()
self.data=json.loads(s.get(url=url,headers=headers).content.decode())
s.keep_alive=False
self.name=[]
self.今天_确诊=[]
self.今天_疑似=[]
self.今天_治愈=[]
self.今天_死亡=[]
self.今天_重症=[]
self.累计_确诊=[]
self.累计_疑似=[]
self.累计_治愈=[]
self.累计_死亡=[]
self.累计_重症=[]
def get_data(self):
date=self.data
self.china_data=date['data']['areaTree'][2]
def get_china_date(self):
name=self.china_data['name']
self.更新时间=self.china_data['lastUpdateTime']
def get_name(self):
for data in self.china_data['children']:
self.name.append(data['name']+'省')
if data['name']!='台湾' and data['name']!='香港':
for data1 in data['children']:
self.name.append(data1['name'])
def get_children(self):
for data in self.china_data['children']:
self.今天_确诊.append(data['today']['confirm'])
self.今天_疑似.append(data['today']['suspect'])
self.今天_治愈.append(data['today']['heal'])
self.今天_死亡.append(data['today']['dead'])
self.今天_重症.append(data['today']['severe'])
self.累计_确诊.append(data['total']['confirm'])
self.累计_疑似.append(data['total']['suspect'])
self.累计_治愈.append(data['total']['heal'])
self.累计_死亡.append(data['total']['dead'])
self.累计_重症.append(data['total']['severe'])
if data['name']!='台湾' and data['name']!='香港':
for data1 in data['children']:
self.今天_确诊.append(data1['today']['confirm'])
self.今天_疑似.append(data1['today']['suspect'])
self.今天_治愈.append(data1['today']['heal'])
self.今天_死亡.append(data1['today']['dead'])
self.今天_重症.append(data1['today']['severe'])
self.累计_确诊.append(data1['total']['confirm'])
self.累计_疑似.append(data1['total']['suspect'])
self.累计_治愈.append(data1['total']['heal'])
self.累计_死亡.append(data1['total']['dead'])
self.累计_重症.append(data1['total']['severe'])
def show(self):
self.all_data=[]
for num in range(0,680):
if self.name[num][-1]=='省':
a=f'{self.name[num]}\n 今天 确诊:{self.今天_确诊[num]} 疑似:{self.今天_疑似[num]} 治愈:{self.今天_治愈[num]} 死亡:{self.今天_死亡[num]} 重症:{self.今天_重症[num]}\n 累计 确诊:{self.累计_确诊[num]} 疑似:{self.累计_疑似[num]} 治愈:{self.累计_治愈[num]} 死亡:{self.累计_死亡[num]}\n'
else:
a=f' {self.name[num]}\n 今天 确诊:{self.今天_确诊[num]} 疑似:{self.今天_疑似[num]} 治愈:{self.今天_治愈[num]} 死亡:{self.今天_死亡[num]} 重症:{self.今天_重症[num]}\n 累计 确诊:{self.累计_确诊[num]} 疑似:{self.累计_疑似[num]} 治愈:{self.累计_治愈[num]} 死亡:{self.累计_死亡[num]}\n'
self.all_data.append(a)
print(a)
def save(self):
if os.path.exists(f'{self.更新时间}.txt'):
pass
else:
file_name=self.更新时间.replace(' ','_')
file_name=file_name.replace(':','_')
with open(f'{file_name}.txt','a+') as f:
for a1 in self.all_data:
f.write(a1)
if __name__ == '__main__':
a=疫情()
a.get_data()
a.get_china_date()
a.get_name()
a.get_children()
a.show()
a.save()