1. 程式人生 > 程式設計 >python小程式基於Jupyter實現天氣查詢的方法

python小程式基於Jupyter實現天氣查詢的方法

天氣查詢python小程式第0步:匯入工具庫第一步:生成查詢天氣的url連結第二步:訪問url連結,解析伺服器返回的json資料,變成python的字典資料第三步:對字典進行索引,獲取氣溫、風速、風向等天氣資訊第四步:遍歷forecast列表中的五個元素,列印天氣資訊完整Python程式碼
本案例是一個非常有趣的python小程式,呼叫網路API查詢指定城市的天氣,並列印輸出天氣資訊。

你將學到以下技能:

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

第0步:匯入工具庫

import urllib.request
import 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.request
import 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']['yesterday']['fl'][-5:-3])
print('--------------------------')
# 第四步:遍歷forecast列表中的五個元素,列印天氣資訊
for each in weather_dict['data']['forecast']:
  print('日期',each['fengli'][-5:-3])
  print('--------------------------')

python小程式基於Jupyter實現天氣查詢的方法

到此這篇關於python小程式基於Jupyter實現天氣查詢的方法的文章就介紹到這了,更多相關python Jupyter 天氣查詢內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!