From 4c81cbfda6db5a3adc2332b8a21d44bb0f296589 Mon Sep 17 00:00:00 2001 From: bc759751 Date: Wed, 1 Jul 2020 14:38:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A9=E6=B0=A3API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 47 +++++++++++++++++++++++++++++++++++++ templates/weather-form.html | 35 +++++++++++++++++++++++++++ templates/weather_post.html | 11 +++++++++ testapi.py | 15 ++++++++++++ weather_data.py | 12 ++++++++++ 5 files changed, 120 insertions(+) create mode 100644 app.py create mode 100644 templates/weather-form.html create mode 100644 templates/weather_post.html create mode 100644 testapi.py create mode 100644 weather_data.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..7c5d003 --- /dev/null +++ b/app.py @@ -0,0 +1,47 @@ +from flask import Flask, render_template, request, json +import requests +# import urllib.request +# from urllib.request import urlopen + +app = Flask(__name__) +@app.route("/") +def index(): + # return ['data', 'AAA'] + return render_template("weather-form.html") + + +@app.route("/datasTaipei", methods=['GET', 'POST']) +def datas(): + if request.method == 'POST': + + 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"] + for i in list_of_dicts3: + print(i["locationName"]) + + return list_of_dicts3 + + + +@app.route("/datasChiayi", methods=['GET', 'POST']) +def datasTaichung(): + 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"] + + return list_of_dicts2 + + +@app.route("/datasKaohsiung", methods=['GET', 'POST']) +def datasKaohsiung(): + 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"] + + return list_of_dicts2 + + +if __name__ == "__main__": + app.run(debug=True) diff --git a/templates/weather-form.html b/templates/weather-form.html new file mode 100644 index 0000000..1eb4f88 --- /dev/null +++ b/templates/weather-form.html @@ -0,0 +1,35 @@ + + + + + + Document + + +

想查詢哪裡的天氣

+ +
+

台北

+ + + + +
+
+

嘉義

+ + + + +
+
+

高雄

+
+
+ +
+ +
+
+ + \ No newline at end of file diff --git a/templates/weather_post.html b/templates/weather_post.html new file mode 100644 index 0000000..d01f779 --- /dev/null +++ b/templates/weather_post.html @@ -0,0 +1,11 @@ + + + + + + Document + + + + + \ No newline at end of file diff --git a/testapi.py b/testapi.py new file mode 100644 index 0000000..a00d155 --- /dev/null +++ b/testapi.py @@ -0,0 +1,15 @@ +import requests +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", verify=False) +list_of_dicts = r.json() + +# print(type(r)) +# print(type(list_of_dicts2)) +new_list = list(list_of_dicts["records"]["locations"]) +# for i in new_list: +# print(i["locationsName"]) +print(new_list) +# print(list_of_dicts) +# for i in list_of_dicts: +# print(i["weatherElement"]) diff --git a/weather_data.py b/weather_data.py new file mode 100644 index 0000000..cf1d29b --- /dev/null +++ b/weather_data.py @@ -0,0 +1,12 @@ +import urllib.request as request +import json +url = "http://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-069?Authorization=CWB-6B764FB3-BD6E-4198-96C0-43FB4EBA1375&limit=5&offset=0&format=JSON&locationName=%E6%9E%97%E5%8F%A3%E5%8D%80&elementName=T" + # return render_template("weather-form.html") +with request.urlopen(url) as response: + data = json.load(response) + clist = data["records"] + clist2 = clist["locations"][0] + clist3 = clist2["location"] +for i in clist3: + for j in clist3: + print(j["time"]) \ No newline at end of file