Browse Source

天氣API

master
bc759751 10 months ago
commit
4c81cbfda6
5 changed files with 120 additions and 0 deletions
  1. +47
    -0
      app.py
  2. +35
    -0
      templates/weather-form.html
  3. +11
    -0
      templates/weather_post.html
  4. +15
    -0
      testapi.py
  5. +12
    -0
      weather_data.py

+ 47
- 0
app.py View File

@ -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)

+ 35
- 0
templates/weather-form.html View File

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h4 class="mt-4 pb-2">想查詢哪裡的天氣</h4>
</div>
<form action="/datasTaipei" method="POST">
<h4 class="mt-4 pb-2">台北</h4>
<input class="btn btn-info" type="submit" value="查詢" />
</div>
</select>
</div>
</form>
<form action="/datasChiayi" method="POST">
<h4 class="mt-4 pb-2">嘉義</h4>
<input class="btn btn-info" type="submit" value="查詢" />
</div>
</select>
</div>
</form>
<form action="/datasKaohsiung" method="POST">
<h4 class="mt-4 pb-2">高雄</h4>
<div class="d-flex">
<div class="mt-4">
<input class="btn btn-info" type="submit" value="查詢" />
</div>
</select>
</div>
</form>
</body>
</html>

+ 11
- 0
templates/weather_post.html View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>

+ 15
- 0
testapi.py View File

@ -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"])

+ 12
- 0
weather_data.py View File

@ -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"])

Loading…
Cancel
Save