温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在python中使用Jupyter实现一个天气查询功能

发布时间:2021-04-16 16:27:45 来源:亿速云 阅读:446 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关怎么在python中使用Jupyter实现一个天气查询功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

第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']['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实现一个天气查询功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI