跳至內容
cwa
使用指南

綜合天氣

一次取得鄉鎮的預報、目前天氣及警特報。

cwa.weather.get() 一次取得一個鄉鎮最常用的三種資料:預報、鄰近測站的目前天氣及生效中的警特報。做天氣 App 或通知時,通常從這裡開始就夠了。

import cwa

weather = cwa.weather.get("臺北市信義區", days=3)

if weather.current:
    print("現在", weather.current.temperature)
if weather.today:
    print("今天降雨機率", weather.today.precipitation.max_probability)
for alert in weather.alerts:
    print(alert.title)

days 與預報相同,指定預報天數,預設為 7。

欄位內容詳細說明
location查詢的鄉鎮指定地點
as_of查詢時間
current目前天氣,見下方;找不到符合條件的測站時為 None觀測值及測站
forecast完整預報預報
today、tomorrow今天及明天的預報,等同 forecast.today、forecast.tomorrow;預報未涵蓋該日時為 None預報
alerts影響此鄉鎮的警特報,可直接以 for 逐一取出警特報

目前天氣

current 取自 10 公里內、兩小時內有觀測值的最近測站,該測站可能位於相鄰鄉鎮。

current 本身可直接取得以下欄位:

欄位說明
current.station回報的測站,例如 current.station.name
current.distance_km測站到查詢鄉鎮代表點的距離(km)
current.observed_at觀測時間
current.temperature氣溫(°C)

濕度、氣壓、風、雨量及天氣現象在 current.observation 中:

if weather.current:
    observation = weather.current.observation
    print(observation.humidity, observation.pressure, observation.wind.speed, observation.precipitation.millimetres)

各欄位的單位及缺值意義見觀測值及測站。需要調整距離或時間條件時,改用 cwa.observations.nearest()。

多個鄉鎮

傳入縣市或清單時,回傳以鄉鎮為鍵的結果;get_all() 查詢全臺所有鄉鎮。見批次查詢與匯出。