可以用的天氣
import requests
import json
import os
import pickle
if not os.path.exists("first_time.pickle"):
? ?key = input("請(qǐng)輸入和風(fēng)天氣key:")
? ?with open("first_time.pickle", "wb") as f:
? ? ? ?pickle.dump(key, f)
else:
? ?with open("first_time.pickle", "rb") as f:
? ? ? ?key = pickle.load(f)
city = input("請(qǐng)輸入你想查詢的城市名稱:")
url = "https://geoapi.qweather.com/v2/city/lookup"
querystring = {"location":city,"key":key}
headers = {
? ?"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
response = requests.request("GET", url, headers=headers, params=querystring)
if "location" in response.json():
? ?city_id = response.json()["location"][0]["id"]
else:
? ?print("獲取城市ID失敗,請(qǐng)檢查你的請(qǐng)求參數(shù)和API Key是否正確。")
? ?exit()
url = "https://devapi.qweather.com/v7/weather/now"
querystring = {"location":city_id,"key":key}
response = requests.request("GET", url, headers=headers, params=querystring)
response = requests.request("GET", url, headers=headers, params=querystring)
data = json.loads(response.text)
code = data["code"]
update_time = data["updateTime"]
fx_link = data["fxLink"]
now_data = data["now"]
obs_time = now_data["obsTime"]
temp = now_data["temp"]
feels_like = now_data["feelsLike"]
icon = now_data["icon"]
text = now_data["text"]
wind360 = now_data["wind360"]
wind_dir = now_data["windDir"]
wind_scale = now_data["windScale"]
wind_speed = now_data["windSpeed"]
humidity = now_data["humidity"]
precip = now_data["precip"]
pressure = now_data["pressure"]
vis = now_data["vis"]
cloud = now_data["cloud"]
dew = now_data["dew"]
print("網(wǎng)頁視圖鏈接",fx_link)
print("天氣更新時(shí)間",obs_time)
print("當(dāng)前溫度為",temp,"℃")
print("體感溫度",feels_like,"℃")
print("天氣狀況",text)
print("風(fēng)向360角度",wind360,"°")
print("風(fēng)向",wind_dir)
print("風(fēng)力等級(jí)",wind_scale)
print("風(fēng)速",wind_speed,"公里/小時(shí)")
print("相對(duì)濕度",humidity,"%")
print("當(dāng)前小時(shí)累計(jì)降水量",precip,"毫米")
print("大氣壓強(qiáng)",pressure,"百帕")
print("能見度",vis,"公里")
print("云量",cloud,"%")
print("露點(diǎn)濕度",dew)