数据来源:https://raw.githubusercontent.com/BlankerL/DXY-2019-nCoV-Data/master/json/DXYArea.json (每日一更)
获取数据并写入JSON文件(Epidemic_data.json):Epidemic_data_url.py
# --*-- coding: UTF-8 --*--
import requests
def get_data_url():
url = "https://raw.githubusercontent.com/BlankerL/DXY-2019-nCoV-Data/master/json/DXYArea.json"
r = requests.get(url).text
with open("Epidemic_data.json", "w", encoding = "UTF-8") as f:
f.write(r)
if __name__ == "__main__":
get_data_url()
处理数据:Epidemic_data.py
# --*-- coding: UTF-8 --*--
import json
def get_data():
with open("Epidemic_data.json", "r", encoding = "UTF-8") as f:
data = json.load(f)
return data
def print_data():
data = get_data()["results"]
print("查询国家总疫情数据请输入国家名称简称,国内只支持省份、城市的数据查询,查询《湖北省》数据请输入:湖北,查询《武汉市》数据请输入:武汉,某些城市带州的请尝试输入:某某州(例如:大理州,神农架林区等),不支持查询《待明确地区》。")
query = input("请输入查询地:")
for i in range(len(data)):
provinceShortName = data[i]["provinceShortName"]
confirmedCount_p = data[i]["confirmedCount"]
currentConfirmedCount_p = data[i]["currentConfirmedCount"]
curedCount_p = data[i]["curedCount"]
deadCount_p = data[i]["deadCount"]
k = data[i]["cities"]
if(query == provinceShortName):
print("{}--确诊人数:".format(str(query)) + str(confirmedCount_p))
print("{}--现存确诊人数:".format(str(query)) + str(currentConfirmedCount_p))
print("{}--治愈人数:".format(str(query)) + str(curedCount_p))
print("{}--死亡人数:".format(str(query)) + str(deadCount_p))
elif(k != [] and k != None):
for j in range(len(k)):
cityName = k[j]["cityName"]
confirmedCount_c = k[j]["confirmedCount"]
currentConfirmedCount_c = k[j]["currentConfirmedCount"]
curedCount_c = k[j]["curedCount"]
deadCount_c = k[j]["deadCount"]
if(query == cityName):
print("{}--确诊人数:".format(str(query)) + str(confirmedCount_c))
print("{}--现存确诊人数:".format(str(query)) + str(currentConfirmedCount_c))
print("{}--治愈人数:".format(str(query)) + str(curedCount_c))
print("{}--死亡人数:".format(str(query)) + str(deadCount_c))
if __name__ == "__main__":
get_data()
print_data()
学习用,不足之处请多指教。