
10家適合網站項目使用的最佳翻譯api平臺
https://devapi.qweather.com/v7/weather/now?
# 商業訂閱用戶
https://api.qweather.com/v7/weather/now?
通過/weather/now
接口獲取當前天氣:
import requests
api_url = "https://devapi.qweather.com/v7/weather/now"
params = {
"location": "101020100", # 上海城市ID
"key": "YOUR_API_KEY" # 替換為實際Key
}
response = requests.get(api_url, params=params)
data = response.json()
if data["code"] == "200":
now = data["now"]
print(f"""當前天氣:
溫度:{now["temp"]}℃
體感:{now["feelsLike"]}℃
天氣:{now["text"]}
濕度:{now["humidity"]}%
""")
else:
print(f"請求失敗:{data['code']}")
關鍵返回字段解析:
temp
:實時溫度(攝氏度)feelsLike
:體感溫度text
:天氣狀況描述(如“多云”)windSpeed
:風速(公里/小時)humidity
:相對濕度(百分比)調用/weather/7d
獲取7天預報:
# 替換請求端點
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']}")
數據應用場景:
使用/minutely/5m
接口實現精準降水預測:
api_url = "https://devapi.qweather.com/v7/minutely/5m"
# 相同參數結構...
返回數據包含未來2小時內每5分鐘的降水強度和類型,時空分辨率達500m×500m,特別適合物流調度、出行App實時提醒。
實時天氣:https://devapi.qweather.com/v7/weather/now?key=KEY&
7日預報:https://devapi.qweather.com/v7/weather/7d?key=KEY&
location
參數綁定前端組件:location={{select1.value}} # 城市選擇組件的值
// 小時溫度折線圖
xAxis: {
data: Array.from(hourlyData, ({fxTime})=>fxTime.slice(11,16))
},
series: [{
data: Array.from(hourlyData, ({temp})=>temp)
}]
Python實現邏輯:
# 郵件內容生成示例
weather_html = "<h2>廣州7日預報</h2><ul>"
for day in forecast_data:
weather_html += f"""
<li>{day['date']}: {day['tempMin']}-{day['tempMax']}℃ {day['textDay']}</li>
"""
weather_html += "</ul>"
關鍵技術點:
schedule
庫管理定時任務四步完成接入:
qweather-icons.css
和字體文件)@font-face {
font-family: 'qweather-icons';
src: url('/static/qweather-icons.ttf');
}
<text class="qi-icon"
:class="isNight ? 'qi-{{item.iconNight}}' : 'qi-{{item.iconDay}}'">
</text>
注意事項:免費版API調用需綁定小程序服務器IP,防止盜用
通過LangChain工具將天氣API接入Qwen-7B-Chat:
def weather_agent(query):
# 1. 從問題中提取城市名(如“上海天氣?”)
city = extract_city(query)
# 2. 調用城市ID映射表
id = city_id_map[city]
# 3. 請求和風API
api_url = f"https://devapi.qweather.com/v7/weather/24h?location={id}&key=KEY"
# 4. 解析數據并生成自然語言描述
return f"當前{data['now']['temp']}℃,{data['now']['text']}。未來24小時..."
應用場景:智能對話系統回答天氣問題,并給出穿衣建議、出行提示等增值信息
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
當主API不可用時,自動切換備用端點:
# 主節點
api1.qweather.com
# 備用節點
api2.qweather.com
code
字段非200時檢查:隨著AI大模型爆發,氣象服務呈現兩大趨勢:
和風天氣已布局的氣象數據湖聯邦學習框架,允許客戶使用私有數據本地訓練模型,在保障數據隱私的同時提升預測精度。
和風天氣API憑借其免費額度充足、數據覆蓋全面、文檔完善的特點,是個人開發者和小型項目的理想起點。對于企業級應用,建議: