自動化天氣查詢工具(API調用)完全指南
天氣數據是日常生活中經常需要查詢的信息之一。本教程將介紹如何使用Python編寫一個自動化天氣查詢工具,通過調用開放的天氣API獲取實時天氣數據。這個工具適合完全不懂編程的新手學習,將從最基礎的環境搭建講起,逐步實現完整的天氣查詢功能。
環境準備
在開始編寫代碼之前,需要確保計算機上已經安裝了Python環境。Python是一種簡單易學的編程語言,非常適合新手入門。可以從Python官網下載最新版本,安裝過程保持默認選項即可。
安裝完成后,打開命令提示符或終端,輸入python --version
檢查是否安裝成功。為了管理項目依賴,還需要安裝pip工具,它是Python的包管理器。多數情況下,Python安裝時會自動包含pip。
需要安裝幾個關鍵的Python庫:requests用于發送HTTP請求,json用于處理JSON格式的數據,datetime用于處理日期時間。這些庫可以通過以下命令安裝:
pip install requests
獲取API密鑰
大多數天氣數據服務都需要API密鑰才能訪問。OpenWeatherMap是一個提供免費天氣API的服務,注冊賬戶后可以獲得免費的API密鑰。訪問OpenWeatherMap官網,完成注冊流程,在個人儀表板中可以找到API密鑰。
免費版的API有一定的調用限制,但對于個人學習和測試完全夠用。API密鑰是一個長字符串,需要妥善保管,不要直接暴露在公開的代碼中。
基本API調用
理解HTTP請求是使用API的核心。API通常通過HTTP協議提供數據,最常見的請求方式是GET。請求需要包含必要的參數,如位置信息、API密鑰等。
使用requests庫發送GET請求非常簡單:
import requestsapi_key = "你的API密鑰"
city = "北京"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"response = requests.get(url)
data = response.json()
這段代碼構造了一個API請求URL,包含城市名稱和API密鑰兩個參數。requests.get()發送請求后,返回的響應可以通過.json()方法轉換為Python字典。
解析天氣數據
API返回的JSON數據包含豐富的天氣信息,需要從中提取關鍵數據展示給用戶。典型的天氣數據包括溫度、濕度、天氣狀況、風速等。
溫度數據默認使用開爾文單位,需要轉換為更常用的攝氏度:
temperature = data["main"]["temp"] - 273.15
humidity = data["main"]["humidity"]
weather_desc = data["weather"][0]["description"]
wind_speed = data["wind"]["speed"]
數據解析后可以格式化為更友好的輸出:
print(f"當前天氣: {weather_desc}")
print(f"溫度: {temperature:.1f}°C")
print(f"濕度: {humidity}%")
print(f"風速: {wind_speed}m/s")
添加用戶交互
基本的天氣查詢功能實現后,可以增加用戶交互使工具更實用。使用input函數讓用戶輸入要查詢的城市:
city = input("請輸入要查詢的城市名稱: ")
為了處理用戶輸入可能導致的錯誤,如城市不存在或API請求失敗,需要添加異常處理:
try:response = requests.get(url)response.raise_for_status()data = response.json()
except requests.exceptions.RequestException as e:print(f"獲取天氣數據失敗: {e}")exit()
美化輸出格式
原始的控制臺輸出比較簡陋,可以通過添加分隔線和顏色來改善顯示效果。雖然控制臺本身不支持富文本,但可以使用ANSI顏色代碼:
print("\033[1;36m====== 天氣查詢結果 ======\033[0m")
print(f"\033[1;33m城市: \033[0m{city}")
print(f"\033[1;33m天氣狀況: \033[0m{weather_desc}")
print(f"\033[1;33m溫度: \033[0m{temperature:.1f}°C")
擴展功能
基礎功能完成后,可以考慮添加更多實用功能。例如,查詢多個城市的天氣、保存查詢歷史、設置溫度單位等。
多城市查詢可以通過循環實現:
cities = ["北京", "上海", "廣州"]
for city in cities:# 查詢每個城市的天氣...
添加歷史記錄功能需要將每次查詢的結果保存到文件或數據庫中。簡單的文件存儲實現:
import json
from datetime import datetimehistory = {"city": city,"temperature": temperature,"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}with open("weather_history.json", "a") as f:f.write(json.dumps(history) + "\n")
完整代碼實現
將上述所有功能整合,形成完整的天氣查詢工具。代碼結構清晰,包含錯誤處理、用戶交互和數據持久化等完整功能。
import requests
import json
from datetime import datetimedef get_weather(api_key, city):url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"try:response = requests.get(url)response.raise_for_status()return response.json()except requests.exceptions.RequestException as e:print(f"獲取天氣數據失敗: {e}")return Nonedef display_weather(data, city):if not data:returntemperature = data["main"]["temp"] - 273.15humidity = data["main"]["humidity"]weather_desc = data["weather"][0]["description"]wind_speed = data["wind"]["speed"]print("\033[1;36m====== 天氣查詢結果 ======\033[0m")print(f"\033[1;33m城市: \033[0m{city}")print(f"\033[1;33m天氣狀況: \033[0m{weather_desc}")print(f"\033[1;33m溫度: \033[0m{temperature:.1f}°C")print(f"\033[1;33m濕度: \033[0m{humidity}%")print(f"\033[1;33m風速: \033[0m{wind_speed}m/s")def save_history(city, temperature):history = {"city": city,"temperature": temperature,"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}with open("weather_history.json", "a") as f:f.write(json.dumps(history) + "\n")def main():api_key = "你的API密鑰"print("\033[1;35m天氣查詢工具\033[0m")while True:city = input("請輸入要查詢的城市名稱(輸入quit退出): ")if city.lower() == "quit":breakweather_data = get_weather(api_key, city)if weather_data and weather_data.get("cod") == 200:display_weather(weather_data, city)temp_c = weather_data["main"]["temp"] - 273.15save_history(city, temp_c)else:print("無法獲取該城市的天氣數據,請檢查城市名稱是否正確")if __name__ == "__main__":main()
部署和使用
完成代碼編寫后,可以將其保存為.py文件(如weather_app.py),通過命令行運行:
python weather_app.py
程序啟動后會提示輸入城市名稱,輸入后即可查看該城市的實時天氣信息。查詢歷史會自動保存到同目錄下的weather_history.json文件中。
進階學習方向
掌握了基礎天氣查詢工具的開發后,可以考慮以下進階方向:
- 開發圖形用戶界面(GUI),使用Tkinter或PyQt等庫
- 將應用部署為Web服務,使用Flask或Django框架
- 添加天氣預報功能,查詢多天預報數據
- 實現異常天氣提醒功能
- 開發手機APP版本,使用Kivy等跨平臺框架
完整源碼
import requests
import json
from datetime import datetimedef get_weather(api_key, city):"""獲取指定城市的天氣數據"""base_url = "http://api.openweathermap.org/data/2.5/weather"params = {"q": city,"appid": api_key,"lang": "zh_cn" # 獲取中文描述的天氣}try:response = requests.get(base_url, params=params)response.raise_for_status() # 檢查請求是否成功return response.json()except requests.exceptions.RequestException as e:print(f"獲取天氣數據失敗: {e}")return Nonedef display_weather(data, city):"""顯示天氣信息"""if not data or data.get("cod") != 200:print("無法獲取有效的天氣數據")return# 解析天氣數據main_data = data["main"]weather_data = data["weather"][0]wind_data = data.get("wind", {})# 單位轉換和數據處理temperature = main_data["temp"] - 273.15 # 開爾文轉攝氏度feels_like = main_data["feels_like"] - 273.15temp_min = main_data["temp_min"] - 273.15temp_max = main_data["temp_max"] - 273.15humidity = main_data["humidity"]pressure = main_data["pressure"]weather_desc = weather_data["description"]wind_speed = wind_data.get("speed", 0)wind_deg = wind_data.get("deg", 0)clouds = data.get("clouds", {}).get("all", 0)visibility = data.get("visibility", "未知")# 風向度數轉換為方向directions = ["北", "東北", "東", "東南", "南", "西南", "西", "西北"]wind_dir = directions[int((wind_deg + 22.5) / 45) % 8] if wind_deg else "未知"# 格式化輸出print("\n\033[1;36m========== 天氣查詢結果 ==========\033[0m")print(f"\033[1;33m城市: \033[0m{city}")print(f"\033[1;33m天氣狀況: \033[0m{weather_desc}")print(f"\033[1;33m當前溫度: \033[0m{temperature:.1f}°C (體感{feels_like:.1f}°C)")print(f"\033[1;33m溫度范圍: \033[0m{temp_min:.1f}°C ~ {temp_max:.1f}°C")print(f"\033[1;33m濕度: \033[0m{humidity}%")print(f"\033[1;33m氣壓: \033[0m{pressure}hPa")print(f"\033[1;33m風速: \033[0m{wind_speed}m/s, 風向: {wind_dir}")print(f"\033[1;33m云量: \033[0m{clouds}%")print(f"\033[1;33m能見度: \033[0m{visibility}m" if visibility != "未知" else "\033[1;33m能見度: \033[0m未知")# 日出日落時間if "sys" in data and "sunrise" in data["sys"] and "sunset" in data["sys"]:sunrise = datetime.fromtimestamp(data["sys"]["sunrise"]).strftime("%H:%M:%S")sunset = datetime.fromtimestamp(data["sys"]["sunset"]).strftime("%H:%M:%S")print(f"\033[1;33m日出: \033[0m{sunrise}")print(f"\033[1;33m日落: \033[0m{sunset}")def save_history(city, weather_info):"""保存查詢歷史到文件"""history = {"city": city,"temperature": weather_info["main"]["temp"] - 273.15,"weather": weather_info["weather"][0]["description"],"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}try:with open("weather_history.json", "a", encoding="utf-8") as f:f.write(json.dumps(history, ensure_ascii=False) + "\n")except IOError as e:print(f"保存歷史記錄失敗: {e}")def show_history():"""顯示查詢歷史"""try:with open("weather_history.json", "r", encoding="utf-8") as f:print("\n\033[1;35m========== 查詢歷史 ==========\033[0m")for line in f:record = json.loads(line.strip())print(f"{record['time']} - {record['city']}: {record['temperature']:.1f}°C, {record['weather']}")except FileNotFoundError:print("\n暫無查詢歷史記錄")except json.JSONDecodeError:print("\n歷史記錄文件損壞")def main():"""主程序"""api_key = "你的API密鑰" # 替換為你的實際API密鑰print("\033[1;35m========== 天氣查詢工具 ==========\033[0m")print("輸入城市名稱查詢天氣")print("輸入'history'查看查詢歷史")print("輸入'quit'或'exit'退出程序\n")while True:user_input = input("請輸入城市名稱或命令: ").strip()if user_input.lower() in ("quit", "exit"):print("感謝使用天氣查詢工具,再見!")breakif user_input.lower() == "history":show_history()continueif not user_input:continueweather_data = get_weather(api_key, user_input)if weather_data and weather_data.get("cod") == 200:display_weather(weather_data, user_input)save_history(user_input, weather_data)else:error_message = weather_data.get("message", "未知錯誤") if weather_data else "請求失敗"print(f"無法獲取天氣數據: {error_message}")if __name__ == "__main__":main()
這個完整版本增加了更多天氣細節的顯示,包括溫度(當前/最高/最低)、濕度、風速、氣壓、能見度、日出日落時間等全面信息。完善了錯誤處理機制,當API請求失敗時,會顯示詳細的錯誤提示而非直接崩潰,比如網絡連接問題、無效的城市名稱或API密鑰錯誤等情況都會得到妥善處理。
特別提供了貼心的查詢歷史功能,系統會自動記錄用戶最近搜索的10個城市,方便快速回溯查看。歷史記錄會以時間倒序排列,并支持一鍵清除功能。為了優化用戶體驗,代碼還實現了本地緩存機制,避免頻繁請求API。
使用前請注意:
- 請確保將代碼中的"你的API密鑰"替換為實際的OpenWeatherMap API密鑰
- 建議在開發環境下測試后再部署到生產環境
- API有調用頻率限制,免費版每分鐘60次,每小時1000次
- 如需獲取歷史天氣數據,需要訂閱付費計劃
示例請求格式:
api_key = "your_actual_api_key_here" # 替換為真實密鑰
city_name = "Beijing"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}&units=metric"