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

第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)

第二步:訪問(wèn)url鏈接,解析服務(wù)器返回的json數(shù)據(jù),變成python的字典數(shù)據(jù)
weather_data = urllib.request.urlopen(url).read()
# 訪問(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)
## 第二步:訪問(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('--------------------------')看完上述內(nèi)容,你們對(duì)怎么在python中使用Jupyter實(shí)現(xiàn)一個(gè)天氣查詢(xún)功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)頁(yè)標(biāo)題:怎么在python中使用Jupyter實(shí)現(xiàn)一個(gè)天氣查詢(xún)功能-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article10/dgpsdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、做網(wǎng)站、企業(yè)建站、移動(dòng)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容