疫情监控(Python)

 1 import requests
 2 import json
 3  
 4 def Down_data():
 5     url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5'
 6     headers = {
 7         'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Mobile Safari/537.36'
 8     }
 9     r = requests.get(url, headers)
10     res = json.loads(r.text)
11     data_res = json.loads(res['data'])
12     return data_res
13  
14 def Parse_data1():
15     data = Down_data()
16     list = ['截至时间:'+str(data['lastUpdateTime'])+'\n'
17           '全国确诊人数:'+str(data['chinaTotal']['confirm'])+'\n'
18           '今日新增确诊:'+str(data['chinaAdd']['confirm'])+'\n'
19           '全国疑似:'+str(data['chinaTotal']['suspect'])+'\n'
20           '今日新增疑似:'+str(data['chinaAdd']['suspect'])+'\n'
21           '全国治愈:'+str(data['chinaTotal']['heal'])+'\n'
22           '今日新增治愈:'+str(data['chinaAdd']['heal'])+'\n'
23           '全国死亡:'+str(data['chinaTotal']['dead'])+'\n'
24           '今日新增死亡:'+str(data['chinaAdd']['dead'])+'\n']
25     result = ''.join(list)
26     with open('疫情查询.txt', 'a+', encoding="utf-8") as f:
27         f.write(result + '\n')
28  
29 def Parse_data2():
30     data = Down_data()['areaTree'][0]['children']
31     path = str(input('请输入你要查询的省份:'))
32     for i in data:
33         if path in i['name']:
34             for item in i['children']:
35                 list_city = [
36                     '地区: '+str(item['name']) + '\n'
37                     ' 确诊人数:' + str(item['total']['confirm']) ,
38                     ' 新增确诊:' + str(item['today']['confirm']) ,
39                     ' 治愈:' + str(item['total']['heal']) ,
40                     ' 新增治愈:' + str(item['today']['heal']) ,
41                     ' 死亡:' + str(item['total']['dead']) ,
42                     ' 新增死亡:' + str(item['today']['dead']) + '\n'
43                             ]
44                 res_city = ''.join(list_city)
45                 with open('疫情查询.txt', 'a+', encoding="utf-8") as f:
46                     f.write(res_city)
47  
48 Down_data()
49 Parse_data1()
50 Parse_data2()

输入省份之后,程序将结束,之后再找个py的同级目录下将生成txt文本文件,里面就是得到的数据!!!

posted @ 2020-03-25 01:01  一斜星辰酱  阅读(540)  评论(0编辑  收藏  举报