• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-07) 36次浏览 已收录 0个评论

这篇文章主要介绍了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)

第二步:访问url链接,解析服务器返回的json数据,变成python的字典数据

 weather_data = urllib.request.urlopen(url).read()
 # 访问url链接,获取字节串数据 weather_data

 # 将字节串解码为unicode编码 weather_data = gzip.decompress(weather_data)
 weather_data
 # 将unicode编码解码为utf-8编码,显示中文 weather_data = weather_data.decode('utf-8')
 weather_data

 # 将字符串两端的引号去掉,变成python中的字典数据 weather_dict = eval(weather_data)
 weather_dict

 type(weather_dict)

第三步:对字典进行索引,获取气温、风速、风向等天气信息

 weather_dict

 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('--------------------------')

第四步:遍历forecast列表中的五个元素,打印天气信息

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

 weather_dict['data']['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代码

 # 导入工具库 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('de<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码网</strong>sc') == '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 天气查询内容请搜索gaodaima搞代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持gaodaima搞代码网

以上就是python小程序基于Jupyter实现天气查询的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python小程序基于Jupyter实现天气查询的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址