鍵.png)
GraphQL API滲透測(cè)試指南
https://devapi.qweather.com/v7/weather/now?
# 商業(yè)訂閱用戶
https://api.qweather.com/v7/weather/now?
通過/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)鍵返回字段解析:
temp
:實(shí)時(shí)溫度(攝氏度)feelsLike
:體感溫度text
:天氣狀況描述(如“多云”)windSpeed
:風(fēng)速(公里/小時(shí))humidity
:相對(duì)濕度(百分比)調(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)景:
使用/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í)時(shí)天氣:https://devapi.qweather.com/v7/weather/now?key=KEY&
7日預(yù)報(bào):https://devapi.qweather.com/v7/weather/7d?key=KEY&
location
參數(shù)綁定前端組件:location={{select1.value}} # 城市選擇組件的值
// 小時(shí)溫度折線圖
xAxis: {
data: Array.from(hourlyData, ({fxTime})=>fxTime.slice(11,16))
},
series: [{
data: Array.from(hourlyData, ({temp})=>temp)
}]
Python實(shí)現(xiàn)邏輯:
# 郵件內(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):
schedule
庫(kù)管理定時(shí)任務(wù)四步完成接入:
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>
注意事項(xiàng):免費(fèi)版API調(diào)用需綁定小程序服務(wù)器IP,防止盜用
通過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)回答天氣問題,并給出穿衣建議、出行提示等增值信息
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
當(dāng)主API不可用時(shí),自動(dòng)切換備用端點(diǎn):
# 主節(jié)點(diǎn)
api1.qweather.com
# 備用節(jié)點(diǎn)
api2.qweather.com
code
字段非200時(shí)檢查:隨著AI大模型爆發(fā),氣象服務(wù)呈現(xiàn)兩大趨勢(shì):
和風(fēng)天氣已布局的氣象數(shù)據(jù)湖聯(lián)邦學(xué)習(xí)框架,允許客戶使用私有數(shù)據(jù)本地訓(xùn)練模型,在保障數(shù)據(jù)隱私的同時(shí)提升預(yù)測(cè)精度。
和風(fēng)天氣API憑借其免費(fèi)額度充足、數(shù)據(jù)覆蓋全面、文檔完善的特點(diǎn),是個(gè)人開發(fā)者和小型項(xiàng)目的理想起點(diǎn)。對(duì)于企業(yè)級(jí)應(yīng)用,建議:
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)