Python: 透過Open Weather Map API抓取天氣資料

https://openweathermap.org/
https://openweathermap.org/current

透過HTTP Request要求的天氣資料可以用城市、城市ID、經緯度、或是郵遞區號(僅限美國)來指定。像是查詢英國倫敦的話:

api.openweathermap.org/data/2.5/weather?q=London
api.openweathermap.org/data/2.5/weather?q=London,uk

以下的Python程式碼透過傳入的經緯度透過api要求天氣資料。這段程式碼寫在我的Line Chatbot裡,當使用者在機器人對話裡打卡的時候回傳當地資料。

import json
import requests
import configparser
import logging

def getWeather(latitude, longitude):
    config = configparser.ConfigParser()
    config.read("config.ini")
    appid = config['openwweather']['key']
    openweatherapiurl = "https://api.openweathermap.org/data/2.5/weather?lat="+latitude+"&lon="+longitude+"&APPID="+appid
    logging.info("[getWeather] request URL:" + openweatherapiurl)
    try:
        response = requests.get(openweatherapiurl)
        data = response.json()
        logging.info(data)
        answer = data['weather'][0]['description'] + ". Temp:" + str(int(data['main']['temp']-273.15))+ " (High "+str(int(data['main']['temp_max']-273.15))+", low " +str(int(data['main']['temp_min']-273.15))+". Humidity: "+str(data['main']['humidity'])+"\n"
        logging.info("[getWeather]:" + answer)
        return answer    
    except:
        return None

透過HTTP Request傳遞查詢地點跟appid之後收到下面的HTTP Response資料,包在Json格式當中,像是以下的形式。
其中氣溫是凱式溫標,需要減去273.15度才是我們習慣的攝氏單位

{
  "coord": { "lon": 121.57, "lat": 25.04 },
  "weather": [
    { "id": 801, "main": "Clouds", "description": "few clouds", "icon": "02d" }
  ],
  "base": "stations",
  "main": {
    "temp": 305.75,
    "pressure": 1005,
    "humidity": 60,
    "temp_min": 303.71,
    "temp_max": 309.15
  },
  "visibility": 10000,
  "wind": { "speed": 7.2, "deg": 280 },
  "clouds": { "all": 20 },
  "dt": 1560929615,
  "sys": {
    "type": 1,
    "id": 7949,
    "message": 0.0071,
    "country": "TW",
    "sunrise": 1560891849,
    "sunset": 1560941141
  },
  "timezone": 28800,
  "id": 1675720,
  "name": "Xianeibu",
  "cod": 200
}
Facebook Comments