一、尋找需要用到的API
在我努力查找之下,我找到了和風天氣這個強大又方便的API。接着在平台上注冊登錄,得到你所需要的key,留以接下來使用
二、用requests獲取所需要的數據
根據免費版的url獲取
url = "https://free-api.heweather.net/s6/weather/%s?location=%s&key=%s" \ % (weather_type, parameters, key) page = requests.get(url) page = json.loads(page.text) # 將獲取的json格式轉化為dict類型,方便處理數據
再利用獲取到的page 這個字典從中提取所需要的一系列信息
比如:可以根據參數ip獲取經緯度
print("經度", page['HeWeather6'][0]['basic']['lon']) print("緯度", page['HeWeather6'][0]['basic']['lat'])
再依據參數自己獲取你想要的信息
必選參數
三、測試代碼
查詢北京的當前天氣情況和地理位置