python小程序基于Jupyter實(shí)現(xiàn)天氣查詢(xún)的方法
天氣查詢(xún)python小程序第0步:導(dǎo)入工具庫(kù)第一步:生成查詢(xún)天氣的url鏈接第二步:訪(fǎng)問(wèn)url鏈接,解析服務(wù)器返回的json數(shù)據(jù),變成python的字典數(shù)據(jù)第三步:對(duì)字典進(jìn)行索引,獲取氣溫、風(fēng)速、風(fēng)向等天氣信息第四步:遍歷forecast列表中的五個(gè)元素,打印天氣信息完整Python代碼
本案例是一個(gè)非常有趣的python小程序,調(diào)用網(wǎng)絡(luò)API查詢(xún)指定城市的天氣,并打印輸出天氣信息。
你將學(xué)到以下技能:
向網(wǎng)絡(luò)API發(fā)起請(qǐng)求,解析和處理服務(wù)器返回的json數(shù)據(jù),可以遷移到各種各樣的API中,如PM2.5查詢(xún),道路擁堵查詢(xún),自然災(zāi)害查詢(xún)等。
python字典數(shù)據(jù)類(lèi)型的常用操作
以下的代碼運(yùn)行在jupyter notebook的開(kāi)發(fā)環(huán)境中,這是python數(shù)據(jù)分析、機(jī)器學(xué)習(xí)、人工智能開(kāi)發(fā)最常用的開(kāi)發(fā)界面,因?yàn)榭梢苑浅7奖愕淖珜?xiě)博客、插入圖片和數(shù)學(xué)公式,并輸出代碼運(yùn)行的中間結(jié)果,強(qiáng)烈建議你學(xué)習(xí)如何使用jupyter notebook。
第0步:導(dǎo)入工具庫(kù)
import urllib.request import gzip
第一步:生成查詢(xún)天氣的url鏈接
city_name = '上海' # 將城市的中文名字編碼成utf-8字符 urllib.parse.quote(city_name) # 將編碼后的城市名拼接在原始鏈接的后面 url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)

第二步:訪(fǎng)問(wèn)url鏈接,解析服務(wù)器返回的json數(shù)據(jù),變成python的字典數(shù)據(jù)
weather_data = urllib.request.urlopen(url).read()
# 訪(fǎng)問(wèn)url鏈接,獲取字節(jié)串?dāng)?shù)據(jù) weather_data

# 將字節(jié)串解碼為unicode編碼 weather_data = gzip.decompress(weather_data)
weather_data

# 將unicode編碼解碼為utf-8編碼,顯示中文
weather_data = weather_data.decode('utf-8')
weather_data

# 將字符串兩端的引號(hào)去掉,變成python中的字典數(shù)據(jù) weather_dict = eval(weather_data)
weather_dict

type(weather_dict)
第三步:對(duì)字典進(jìn)行索引,獲取氣溫、風(fēng)速、風(fēng)向等天氣信息
weather_dict

weather_dict['data']['yesterday']['high']
print('您查詢(xún)的城市:',weather_dict['data']['city'])
print('--------------------------')
print('今天的天氣')
print('溫度',weather_dict['data']['wendu'])
print('感冒指數(shù)',weather_dict['data']['ganmao'])
print('--------------------------')
print('昨天的天氣')
print('昨天:',weather_dict['data']['yesterday']['date'])
print('天氣:',weather_dict['data']['yesterday']['type'])
print('最高氣溫:',weather_dict['data']['yesterday']['high'])
print('最低氣溫:',weather_dict['data']['yesterday']['low'])
print('風(fēng)向:',weather_dict['data']['yesterday']['fx'])
print('風(fēng)力:',weather_dict['data']['yesterday']['fl'][-5:-3])
print('--------------------------')

第四步:遍歷forecast列表中的五個(gè)元素,打印天氣信息
weather_dict[‘data'][‘forecast']是一個(gè)包含五個(gè)元素的列表,每一個(gè)元素都是一個(gè)字典。
weather_dict['data']['forecast']

for each in weather_dict['data']['forecast']:
print('日期',each['date'])
print('天氣',each['type'])
print(each['high'])
print(each['low'])
print('風(fēng)向',each['fengxiang'])
print('風(fēng)力:',each['fengli'][-5:-3])
print('--------------------------')

完整Python代碼
# 導(dǎo)入工具庫(kù)
import urllib.request
import gzip
## 第一步:生成查詢(xún)天氣的url鏈接
city_name = input('請(qǐng)輸入要查詢(xún)的城市名稱(chēng):')
# 將城市的中文名字編碼成utf-8字符
urllib.parse.quote(city_name)
# 生成完整url鏈接
url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
## 第二步:訪(fǎng)問(wèn)url鏈接,解析服務(wù)器返回的json數(shù)據(jù),變成python的字典數(shù)據(jù)
# 獲取服務(wù)器返回的json字節(jié)串?dāng)?shù)據(jù)
weather_data = urllib.request.urlopen(url).read()
# 將字節(jié)串?dāng)?shù)據(jù)解碼為unicode中的utf-8數(shù)據(jù)
weather_data = gzip.decompress(weather_data).decode('utf-8')
# 將json數(shù)據(jù)轉(zhuǎn)為python的字典數(shù)據(jù)
weather_dict = eval(weather_data)
if weather_dict.get('desc') == 'invilad-citykey':
print('您輸入的城市未收錄')
# 第三步:對(duì)字典進(jìn)行索引,獲取氣溫、風(fēng)速、風(fēng)向等天氣信息
print('您查詢(xún)的城市:',weather_dict['data']['city'])
print('--------------------------')
print('今天的天氣')
print('溫度',weather_dict['data']['wendu'])
print('感冒指數(shù)',weather_dict['data']['ganmao'])
print('--------------------------')
print('昨天的天氣')
print('昨天:',weather_dict['data']['yesterday']['date'])
print('天氣:',weather_dict['data']['yesterday']['type'])
print('最高氣溫:',weather_dict['data']['yesterday']['high'])
print('最低氣溫:',weather_dict['data']['yesterday']['low'])
print('風(fēng)向:',weather_dict['data']['yesterday']['fx'])
print('風(fēng)力:',weather_dict['data']['yesterday']['fl'][-5:-3])
print('--------------------------')
# 第四步:遍歷forecast列表中的五個(gè)元素,打印天氣信息
for each in weather_dict['data']['forecast']:
print('日期',each['date'])
print('天氣',each['type'])
print(each['high'])
print(each['low'])
print('風(fēng)向',each['fengxiang'])
print('風(fēng)力:',each['fengli'][-5:-3])
print('--------------------------')

到此這篇關(guān)于python小程序基于Jupyter實(shí)現(xiàn)天氣查詢(xún)的方法的文章就介紹到這了,更多相關(guān)python Jupyter 天氣查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)pandas的dataframe繪圖并保存的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇對(duì)pandas的dataframe繪圖并保存的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
django中related_name的用法說(shuō)明
這篇文章主要介紹了django中related_name的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
基于Pytorch的神經(jīng)網(wǎng)絡(luò)之Regression的實(shí)現(xiàn)
本文主要介紹了基于Pytorch的神經(jīng)網(wǎng)絡(luò)之Regression的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Python----數(shù)據(jù)預(yù)處理代碼實(shí)例
這篇文章主要介紹了Python數(shù)據(jù)預(yù)處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
pycharm終端安裝pytorch失敗的問(wèn)題及解決
這篇文章主要介紹了pycharm終端安裝pytorch失敗的問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
利用Python小工具實(shí)現(xiàn)3秒鐘將視頻轉(zhuǎn)換為音頻
這篇文章主要介紹了利用Python小工具實(shí)現(xiàn) 3秒鐘將視頻轉(zhuǎn)換為音頻效果,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
python函數(shù)常見(jiàn)關(guān)鍵字分享
這篇文章主要向大家介紹的是python函數(shù)常見(jiàn)關(guān)鍵字,文章基于python的相關(guān)資料展開(kāi)對(duì)文章主題的詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04

