程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python獲取各個城市疫情數據

編輯:Python
大家好,我要獲取各個城市的疫情數據,這個循環怎麼改呀,第一次學python
import requests

import json

新建存儲數據的字典

res_list = {}
req = requests.get('https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=diseaseh5Shelf%27)
req_data = json.loads(req.text).get('data')
req_data = req_data.get('diseaseh5Shelf')
print('第2個接口json驗證通過')

根據需要,獲取相關數據

lastupdatetime = req_data['lastUpdateTime']
chinatotal = req_data['chinaTotal']
chinaadd = req_data['chinaAdd']
areaTree = req_data['areaTree'][0]
res_list['lastUpdateTime'] = lastupdatetime
res_list['chinaTotal'] = chinatotal
res_list['chinaAdd'] = chinaadd
res_list['citydata'] = []

循環獲取各個地區的數據

#for i_areaTree in areaTree['children']:

i_areaTree = i_areaTree

prov_name = i_areaTree['name']

for i_areaTree_end in i_areaTree['children']:
city_name = i_areaTree_end['name']
if city_name in ['北海']:
print(city_name)
#if prov_name in ['北海']:

for i_areaTree_end in i_areaTree['children']:
i_city_data = {}
i_city_data['prov_name'] = prov_name
i_city_data['city_name'] = i_areaTree_end['name']
i_city_data['local_confirm_add'] = i_areaTree_end['today']['local_confirm_add']#新增本土
i_city_data['wzz_add'] = i_areaTree_end['today']['wzz_add']#新增無症狀
i_city_data['mediumRiskAreaNum'] = i_areaTree_end['total']['mediumRiskAreaNum']#中風險地區個數
i_city_data['highRiskAreaNum'] = i_areaTree_end['total']['highRiskAreaNum']#中風險地區個數
i_city_data['suspect'] = 0
i_city_data['dead'] = i_areaTree_end['total']['dead']
i_city_data['heal'] = i_areaTree_end['total']['heal']
i_city_data['grade'] = '' if (i_areaTree_end['total'].get('grade') is None) else
i_areaTree_end['total']['grade']
res_list['citydata'].append(i_city_data)

print(res_list)

![img](

我https://img-mid.csdnimg.cn/release/static/image/mid/ask/173648956856143.png "#left")

的解答思路和嘗試過的方法


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved