python小程序基于Jupyter实现天气查询的方法

2020-09-24 0 974

天气查询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\'][\'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 python小程序基于Jupyter实现天气查询的方法 http://www.ox520.com/25136.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务