https://devapi.qweather.com/v7/weather/now?

# 商業(yè)訂閱用戶
https://api.qweather.com/v7/weather/now?

三、核心API接口詳解與代碼實(shí)戰(zhàn)

3.1 實(shí)時(shí)天氣數(shù)據(jù)獲取

通過/weather/now接口獲取當(dāng)前天氣:

import requests

api_url = "https://devapi.qweather.com/v7/weather/now"
params = {
"location": "101020100", # 上海城市ID
"key": "YOUR_API_KEY" # 替換為實(shí)際Key
}

response = requests.get(api_url, params=params)
data = response.json()

if data["code"] == "200":
now = data["now"]
print(f"""當(dāng)前天氣:
溫度:{now["temp"]}℃
體感:{now["feelsLike"]}℃
天氣:{now["text"]}
濕度:{now["humidity"]}%
""")
else:
print(f"請(qǐng)求失敗:{data['code']}")

關(guān)鍵返回字段解析

3.2 多日天氣預(yù)報(bào)

調(diào)用/weather/7d獲取7天預(yù)報(bào):

# 替換請(qǐng)求端點(diǎn)
api_url = "https://devapi.qweather.com/v7/weather/7d"

response = requests.get(api_url, params=params)
data = response.json()

if data["code"] == "200":
for day in data["daily"]:
print(f"{day['fxDate']}: {day['tempMin']}~{day['tempMax']}℃, {day['textDay']}")

數(shù)據(jù)應(yīng)用場(chǎng)景

3.3 分鐘級(jí)降水預(yù)報(bào)

使用/minutely/5m接口實(shí)現(xiàn)精準(zhǔn)降水預(yù)測(cè):

api_url = "https://devapi.qweather.com/v7/minutely/5m"
# 相同參數(shù)結(jié)構(gòu)...

返回?cái)?shù)據(jù)包含未來2小時(shí)內(nèi)每5分鐘的降水強(qiáng)度和類型,時(shí)空分辨率達(dá)500m×500m,特別適合物流調(diào)度、出行App實(shí)時(shí)提醒。

四、四大實(shí)戰(zhàn)應(yīng)用場(chǎng)景

4.1 10分鐘構(gòu)建天氣數(shù)據(jù)看板(卡拉云+和風(fēng)API)

  1. 在卡拉云添加數(shù)據(jù)源:
實(shí)時(shí)天氣:https://devapi.qweather.com/v7/weather/now?key=KEY&
7日預(yù)報(bào):https://devapi.qweather.com/v7/weather/7d?key=KEY&
  1. 創(chuàng)建查詢并將location參數(shù)綁定前端組件:
location={{select1.value}}  # 城市選擇組件的值
  1. 使用ECharts語(yǔ)法可視化數(shù)據(jù):
// 小時(shí)溫度折線圖
xAxis: {
data: Array.from(hourlyData, ({fxTime})=>fxTime.slice(11,16))
},
series: [{
data: Array.from(hourlyData, ({temp})=>temp)
}]

4.2 自動(dòng)天氣預(yù)報(bào)郵件系統(tǒng)

Python實(shí)現(xiàn)邏輯

  1. 定時(shí)任務(wù)(8:00觸發(fā))
  2. 調(diào)用和風(fēng)API獲取7天預(yù)報(bào)
  3. 拼接HTML郵件內(nèi)容
  4. SMTP發(fā)送至指定郵箱
# 郵件內(nèi)容生成示例
weather_html = "<h2>廣州7日預(yù)報(bào)</h2><ul>"
for day in forecast_data:
weather_html += f"""
<li>{day['date']}: {day['tempMin']}-{day['tempMax']}℃ {day['textDay']}</li>
"""
weather_html += "</ul>"

關(guān)鍵技術(shù)點(diǎn)

4.3 小程序集成方案

四步完成接入

  1. 下載官方天氣圖標(biāo)庫(kù)(含qweather-icons.css和字體文件)
  2. 在小程序全局樣式中引入:
@font-face {
font-family: 'qweather-icons';
src: url('/static/qweather-icons.ttf');
}
  1. 組件調(diào)用天氣API(需配置域名白名單)
  2. 動(dòng)態(tài)渲染圖標(biāo):
<text class="qi-icon" 
:class="isNight ? 'qi-{{item.iconNight}}' : 'qi-{{item.iconDay}}'">
</text>

注意事項(xiàng):免費(fèi)版API調(diào)用需綁定小程序服務(wù)器IP,防止盜用

4.4 大模型Agent集成

通過LangChain工具將天氣API接入Qwen-7B-Chat:

def weather_agent(query):
# 1. 從問題中提取城市名(如“上海天氣?”)
city = extract_city(query)

# 2. 調(diào)用城市ID映射表
id = city_id_map[city]

# 3. 請(qǐng)求和風(fēng)API
api_url = f"https://devapi.qweather.com/v7/weather/24h?location={id}&key=KEY"

# 4. 解析數(shù)據(jù)并生成自然語(yǔ)言描述
return f"當(dāng)前{data['now']['temp']}℃,{data['now']['text']}。未來24小時(shí)..."

應(yīng)用場(chǎng)景:智能對(duì)話系統(tǒng)回答天氣問題,并給出穿衣建議出行提示等增值信息

五、性能優(yōu)化與最佳實(shí)踐

5.1 請(qǐng)求頻率控制策略

from cachetools import TTLCache
weather_cache = TTLCache(maxsize=100, ttl=1800) # 30分鐘緩存

def get_weather(location):
if location in weather_cache:
return weather_cache[location]

data = fetch_from_api(location)
weather_cache[location] = data
return data

5.2 多節(jié)點(diǎn)災(zāi)備方案

當(dāng)主API不可用時(shí),自動(dòng)切換備用端點(diǎn):

# 主節(jié)點(diǎn)
api1.qweather.com
# 備用節(jié)點(diǎn)
api2.qweather.com

5.3 錯(cuò)誤處理關(guān)鍵點(diǎn)

六、未來展望:氣象API的技術(shù)演進(jìn)

隨著AI大模型爆發(fā),氣象服務(wù)呈現(xiàn)兩大趨勢(shì):

  1. 多源數(shù)據(jù)融合:衛(wèi)星、雷達(dá)、地面觀測(cè)站與用戶實(shí)景照片的眾包校正成為標(biāo)配
  2. 行業(yè)方案精細(xì)化

和風(fēng)天氣已布局的氣象數(shù)據(jù)湖聯(lián)邦學(xué)習(xí)框架,允許客戶使用私有數(shù)據(jù)本地訓(xùn)練模型,在保障數(shù)據(jù)隱私的同時(shí)提升預(yù)測(cè)精度。

總結(jié)

和風(fēng)天氣API憑借其免費(fèi)額度充足數(shù)據(jù)覆蓋全面文檔完善的特點(diǎn),是個(gè)人開發(fā)者和小型項(xiàng)目的理想起點(diǎn)。對(duì)于企業(yè)級(jí)應(yīng)用,建議:

  1. 先免費(fèi)版原型驗(yàn)證:用最小可行產(chǎn)品(MVP)測(cè)試核心功能
  2. 按需升級(jí)訂閱:當(dāng)用戶量增長(zhǎng)或需要更高精度數(shù)據(jù)時(shí),選擇標(biāo)準(zhǔn)版/高級(jí)版
  3. 關(guān)注行業(yè)方案:物流、航空等領(lǐng)域可探索和風(fēng)的定制API套件

上一篇:

深入解析心知天氣API:高精度氣象數(shù)據(jù)賦能智能應(yīng)用開發(fā)

下一篇:

丫丫天氣API VS 彩云天氣API:2025年中國(guó)氣象服務(wù)技術(shù)深度評(píng)測(cè)
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊(cè)

多API并行試用

數(shù)據(jù)驅(qū)動(dòng)選型,提升決策效率

查看全部API→
??

熱門場(chǎng)景實(shí)測(cè),選對(duì)API

#AI文本生成大模型API

對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)

#AI深度推理大模型API

對(duì)比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個(gè)渠道
一鍵對(duì)比試用API 限時(shí)免費(fèi)