欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

10余年創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè),由一走到現(xiàn)在,當(dāng)中離不開團隊頑強的創(chuàng)業(yè)精神,離不開伴隨我們同行的客戶與專業(yè)的合作伙伴,創(chuàng)力信息一直秉承以“見一個客戶,了解一個行業(yè),交一個朋友”的方式為經(jīng)營理念,提出“讓每一個客戶成為我們的終身客戶”為目標(biāo),以為用戶提供精細化服務(wù),全面滿足用戶需求為宗旨,誠信經(jīng)營,更大限度為用戶創(chuàng)造價值。期待邁向下一個更好的10余年。

第0步:導(dǎo)入工具庫

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實現(xiàn)一個天氣查詢功能

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

weather_data = urllib.request.urlopen(url).read()
# 訪問url鏈接,獲取字節(jié)串?dāng)?shù)據(jù)
weather_data

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能

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

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能

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

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能

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

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能

type(weather_dict)

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

weather_dict

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能

weather_dict['data']['yesterday']['high']
print('您查詢的城市:',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('--------------------------')

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能

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

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

weather_dict['data']['forecast']

怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能

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中使用Jupyter實現(xiàn)一個天氣查詢功能

完整Python代碼

# 導(dǎo)入工具庫
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鏈接,解析服務(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('您輸入的城市未收錄')
  
# 第三步:對字典進行索引,獲取氣溫、風(fēng)速、風(fēng)向等天氣信息
print('您查詢的城市:',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列表中的五個元素,打印天氣信息
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('--------------------------')

看完上述內(nèi)容,你們對怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁標(biāo)題:怎么在python中使用Jupyter實現(xiàn)一個天氣查詢功能-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article10/dgpsdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、做網(wǎng)站企業(yè)建站、移動網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司