diff --git a/app.py b/app.py index c81d9ef..e648d05 100644 --- a/app.py +++ b/app.py @@ -18,10 +18,15 @@ def datas(): list_of_dicts = r.json() list_of_dicts2 = list_of_dicts["records"]["locations"][0] list_of_dicts3 = list_of_dicts2["location"] - for i in list_of_dicts3: - print(i["locationName"]) - - return list_of_dicts2 + location = list_of_dicts3[0]["locationName"] + location_name = list_of_dicts3[0] + list_of_dicts5 = location_name["weatherElement"][0] + list_of_dicts6 = list_of_dicts5["time"] + list_of_dicts7 = list_of_dicts6[0]["dataTime"] + Tv = list_of_dicts6[0]["elementValue"][0]["measures"] + Tm = list_of_dicts6[0]["elementValue"][0]["value"] + print(Tm) + return render_template('weather_data.html', name=location, weather=list_of_dicts7, T=Tv, Tm=Tm ) diff --git a/testapi.py b/testapi.py index 03e96ff..11d135e 100644 --- a/testapi.py +++ b/testapi.py @@ -4,15 +4,21 @@ import json r = requests.get("http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-061?Authorization=CWB-6B764FB3-BD6E-4198-96C0-43FB4EBA1375&limit=10&offset=0&format=JSON&elementName=T&sort=time") list_of_dicts = r.json() list_of_dicts2 = list_of_dicts["records"]["locations"][0] -list_of_dicts3 = list_of_dicts2["location"] -#形成列表 北投區為[0] 松山區[1] 士林區[2] 內湖區[3] 中山區[4] 中正區為[5] 信義區為[6] 大安區[7] 文山區[8] 南港區[9] +list_of_dicts3 = list_of_dicts2["location"] +# 形成列表 北投區為[0] 松山區[1] 士林區[2] 內湖區[3] 中山區[4] 中正區為[5] 信義區為[6] 大安區[7] 文山區[8] 南港區[9] # for i in list_of_dicts3: # print(i["locationName"]) -list_of_dicts4 = list_of_dicts3[1] -list_of_dicts5 = list_of_dicts4["weatherElement"][0] +print("請輸入地區:(數字)") +x = input("北投區為[0] 松山區[1] 士林區[2] 內湖區[3] 中山區[4] 中正區為[5] 信義區為[6] 大安區[7] 文山區[8] 南港區[9]:") +y = int(x) +# for i in list_of_dicts3:") +location_name = list_of_dicts3[y] +list_of_dicts5 = location_name["weatherElement"][0] list_of_dicts6 = list_of_dicts5["time"] # for i in list_of_dicts6: -# print(i["dataTime"]) -#此列表為每三個小時的時間 -for i in list_of_dicts6: - print(i["elementValue"][0]) \ No newline at end of file +# print(i["dataTime"]) +# 此列表為每三個小時的時間 +# for i in list_of_dicts6: +# print(i["elementValue"][0]) +print(list_of_dicts3[y]["locationName"]) +print(list_of_dicts6[0])