亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python小程序基于Jupyter實現天氣查詢的方法

瀏覽:51日期:2022-07-31 18:36:34

天氣查詢python小程序第0步:導入工具庫第一步:生成查詢天氣的url鏈接第二步:訪問url鏈接,解析服務器返回的json數據,變成python的字典數據第三步:對字典進行索引,獲取氣溫、風速、風向等天氣信息第四步:遍歷forecast列表中的五個元素,打印天氣信息完整Python代碼本案例是一個非常有趣的python小程序,調用網絡API查詢指定城市的天氣,并打印輸出天氣信息。

你將學到以下技能:

向網絡API發起請求,解析和處理服務器返回的json數據,可以遷移到各種各樣的API中,如PM2.5查詢,道路擁堵查詢,自然災害查詢等。python字典數據類型的常用操作以下的代碼運行在jupyter notebook的開發環境中,這是python數據分析、機器學習、人工智能開發最常用的開發界面,因為可以非常方便的撰寫博客、插入圖片和數學公式,并輸出代碼運行的中間結果,強烈建議你學習如何使用jupyter notebook。

第0步:導入工具庫

import urllib.requestimport gzip

第一步:生成查詢天氣的url鏈接

city_name = ’上?!? 將城市的中文名字編碼成utf-8字符urllib.parse.quote(city_name)# 將編碼后的城市名拼接在原始鏈接的后面url = ’http://wthrcdn.etouch.cn/weather_mini?city=’ + urllib.parse.quote(city_name)

python小程序基于Jupyter實現天氣查詢的方法

第二步:訪問url鏈接,解析服務器返回的json數據,變成python的字典數據

weather_data = urllib.request.urlopen(url).read()

# 訪問url鏈接,獲取字節串數據weather_data

python小程序基于Jupyter實現天氣查詢的方法

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

weather_data

python小程序基于Jupyter實現天氣查詢的方法

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

weather_data

python小程序基于Jupyter實現天氣查詢的方法

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

weather_dict

python小程序基于Jupyter實現天氣查詢的方法

type(weather_dict)

第三步:對字典進行索引,獲取氣溫、風速、風向等天氣信息

weather_dict

python小程序基于Jupyter實現天氣查詢的方法

weather_dict[’data’][’yesterday’][’high’]

print(’您查詢的城市:’,weather_dict[’data’][’city’])print(’--------------------------’)print(’今天的天氣’)print(’溫度’,weather_dict[’data’][’wendu’])print(’感冒指數’,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(’風向:’,weather_dict[’data’][’yesterday’][’fx’])print(’風力:’,weather_dict[’data’][’yesterday’][’fl’][-5:-3])print(’--------------------------’)

python小程序基于Jupyter實現天氣查詢的方法

第四步:遍歷forecast列表中的五個元素,打印天氣信息

weather_dict[‘data’][‘forecast’]是一個包含五個元素的列表,每一個元素都是一個字典。

weather_dict[’data’][’forecast’]

python小程序基于Jupyter實現天氣查詢的方法

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

python小程序基于Jupyter實現天氣查詢的方法

完整Python代碼

# 導入工具庫import urllib.requestimport gzip## 第一步:生成查詢天氣的url鏈接city_name = input(’請輸入要查詢的城市名稱:’)# 將城市的中文名字編碼成utf-8字符urllib.parse.quote(city_name)# 生成完整url鏈接url = ’http://wthrcdn.etouch.cn/weather_mini?city=’+urllib.parse.quote(city_name)## 第二步:訪問url鏈接,解析服務器返回的json數據,變成python的字典數據# 獲取服務器返回的json字節串數據weather_data = urllib.request.urlopen(url).read()# 將字節串數據解碼為unicode中的utf-8數據weather_data = gzip.decompress(weather_data).decode(’utf-8’)# 將json數據轉為python的字典數據weather_dict = eval(weather_data)if weather_dict.get(’desc’) == ’invilad-citykey’: print(’您輸入的城市未收錄’) # 第三步:對字典進行索引,獲取氣溫、風速、風向等天氣信息print(’您查詢的城市:’,weather_dict[’data’][’city’])print(’--------------------------’)print(’今天的天氣’)print(’溫度’,weather_dict[’data’][’wendu’])print(’感冒指數’,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(’風向:’,weather_dict[’data’][’yesterday’][’fx’])print(’風力:’,weather_dict[’data’][’yesterday’][’fl’][-5:-3])print(’--------------------------’)# 第四步:遍歷forecast列表中的五個元素,打印天氣信息for each in weather_dict[’data’][’forecast’]: print(’日期’,each[’date’]) print(’天氣’,each[’type’]) print(each[’high’]) print(each[’low’]) print(’風向’,each[’fengxiang’]) print(’風力:’,each[’fengli’][-5:-3]) print(’--------------------------’)

python小程序基于Jupyter實現天氣查詢的方法

到此這篇關于python小程序基于Jupyter實現天氣查詢的方法的文章就介紹到這了,更多相關python Jupyter 天氣查詢內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产成人99久久亚洲综合精品 | 上海麻豆文化传媒网站入口 | 国产99久久精品一区二区 | 2020亚洲欧美日韩在线观看 | 亚洲福利一区二区 | 外国黄色毛片 | 久久免费视频精品 | 亚洲欧美日韩色 | 草草免费观看视频在线 | 制服丝袜在线看 | 亚洲欧美日韩国产专区一区 | 欧美亚洲国产精品久久蜜芽 | 91热久久免费精品99 | 露脸超嫩97后在线播放 | 性欧美孕妇孕交tv | 可以免费观看欧美一级毛片 | 日韩不卡高清视频 | 国产老肥熟xxxx | 五月天堂婷婷 | 青草免费观看 | 99久久久久国产精品免费 | 黄色带三级1| 青青青免费视频精品99 | 被免费网站在线视频 | 国产精品国产三级国产专区5o | 精品一区精品二区 | 亚洲激情视频 | 香蕉免费一区二区三区在线观看 | 一级做a免费观看大全 | 国产网曝手机视频在线观看 | 特黄女一级毛片 | 国产精品亚洲综合五月天 | 国产精品久久久久999 | 国产亚洲区 | 欧美a一级| 丁香五六月婷婷 | 亚洲一区二区天海翼 | 国产一区二区精品久久 | 欧美精品二区 | 1024免费福利永久观看网站 | 日本中文字幕不卡免费视频 |