鱼C论坛

 找回密码
 立即注册
查看: 1950|回复: 3

[技术交流] python查询天气

[复制链接]
发表于 2020-3-26 17:28:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
python查询天气第三方库:time,urllib.request,gzip,json
  1. import time
  2. import urllib.request
  3. import gzip
  4. import json
  5. print('------天气查询------')
  6. def get_weather_data() :
  7.     city_name = input('请输入要查询的城市名称:')
  8.     url1 = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
  9.     url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
  10.     #网址1只需要输入城市名,网址2需要输入城市代码
  11.     #print(urllib.parse.quote(city_name))
  12.     weather_data = urllib.request.urlopen(url1).read()
  13.     #读取网页数据
  14.     weather_data = gzip.decompress(weather_data).decode('utf-8')
  15.     #解压网页数据
  16.     weather_dict = json.loads(weather_data)
  17.     #将json数据转换为dict数据
  18.     return weather_dict

  19. def show_weather(weather_data):
  20.     weather_dict = weather_data
  21.     #将json数据转换为dict数据
  22.     if weather_dict.get('desc') == 'invilad-citykey':
  23.         print('你输入的城市名有误,或者天气中心未收录你所在城市')
  24.     elif weather_dict.get('desc') =='OK':
  25.         forecast = weather_dict.get('data').get('forecast')
  26.         print('城市:',weather_dict.get('data').get('city'))
  27.         print('温度:',weather_dict.get('data').get('wendu')+'℃ ')
  28.         print('感冒:',weather_dict.get('data').get('ganmao'))
  29.         print('风向:',forecast[0].get('fengxiang'))
  30.         print('风级:',forecast[0].get('fengli'))
  31.         print('高温:',forecast[0].get('high'))
  32.         print('低温:',forecast[0].get('low'))
  33.         print('天气:',forecast[0].get('type'))
  34.         print('日期:',forecast[0].get('date'))
  35.         print('*******************************')
  36.         four_day_forecast =input('是否要显示未来四天天气,是/否:')
  37.         if four_day_forecast == '是' or four_day_forecast == 'Y' or four_day_forecast == 'y':
  38.             for i in range(1,5):
  39.                 print('日期:',forecast[i].get('date'))
  40.                 print('风向:',forecast[i].get('fengxiang'))
  41.                 print('风级:',forecast[i].get('fengli'))
  42.                 print('高温:',forecast[i].get('high'))
  43.                 print('低温:',forecast[i].get('low'))
  44.                 print('天气:',forecast[i].get('type'))
  45.                 print('--------------------------')
  46.     print('***********************************')
  47.     time.sleep(10)

  48. show_weather(get_weather_data())
复制代码
欢迎大家在评论区留言

评分

参与人数 1荣誉 +3 鱼币 +5 贡献 +3 收起 理由
WangJS + 3 + 5 + 3 感谢楼主无私奉献!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-26 18:47:54 | 显示全部楼层
写的真不错(反正我也看不懂,能用就是好!)
沙发我领了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-26 20:21:56 | 显示全部楼层
写的是真棒爱了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-5 16:46:23 | 显示全部楼层
赞,可以运行,拜读一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 19:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表