鱼C论坛

 找回密码
 立即注册
查看: 1771|回复: 4

谁有查询天气的哪个源代码发一下 谢谢

[复制链接]
发表于 2019-1-25 19:40:48 | 显示全部楼层 |阅读模式

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

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

x
谁有查询天气的哪个源代码发一下  谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-25 20:04:36 | 显示全部楼层
  1. import urllib.request
  2. import gzip
  3. import json
  4. print('------天气查询------')
  5. def get_weather_data() :
  6.     city_name = input('请输入要查询的城市名称:')
  7.     url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
  8.     url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
  9.     #网址1只需要输入城市名,网址2需要输入城市代码
  10.     #print(url1)
  11.     weather_data = urllib.request.urlopen(url1).read()
  12.     #读取网页数据
  13.     weather_data = gzip.decompress(weather_data).decode('utf-8')
  14.     #解压网页数据
  15.     weather_dict = json.loads(weather_data)
  16.     #将json数据转换为dict数据
  17.     return weather_dict

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

  46. show_weather(get_weather_data())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-1-26 16:48:19 | 显示全部楼层
  1. import urllib.request
  2. import gzip
  3. import json
  4. print('------天气查询------')
  5. def get_weather_data() :
  6.     city_name = input('请输入要查询的城市名称(直接敲回车则默认为龙川):')
  7.     if city_name == '':
  8.         city_name = '龙川'
  9.     url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
  10.     url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
  11.     #网址1只需要输入城市名,网址2需要输入城市代码
  12.     #print(url1)
  13.     weather_data = urllib.request.urlopen(url1).read()
  14.     #读取网页数据
  15.     weather_data = gzip.decompress(weather_data).decode('utf-8')
  16.     #解压网页数据
  17.     weather_dict = json.loads(weather_data)
  18.     #将json数据转换为dict数据
  19.     return weather_dict

  20. def show_weather(weather_data):
  21.     weather_dict = weather_data
  22.     #将json数据转换为dict数据
  23.     if weather_dict.get('desc') == 'invilad-citykey':
  24.         print('您输入的城市名有误,或者天气中心暂未收录您所在城市!')
  25.     elif weather_dict.get('desc') =='OK':
  26.         forecast = weather_dict.get('data').get('forecast')
  27.         print('您输入的城市:',weather_dict.get('data').get('city'))
  28.         print('即时或大致温度:',weather_dict.get('data').get('wendu')+'℃ ')
  29.         print('感冒概率:',weather_dict.get('data').get('ganmao'))
  30.         print('风向(风吹来的方向):',forecast[0].get('fengxiang'))
  31.         print('风级(无需理会代码,只看“数字+‘级’”):',forecast[0].get('fengli'))
  32.         print('最高温度(无需理会“高温”二字):',forecast[0].get('high'))
  33.         print('最低温度(无需理会“低温”二字):',forecast[0].get('low'))
  34.         print('现在或今日大致天气:',forecast[0].get('type'))
  35.         print('当前日期:',forecast[0].get('date'))
  36.         print('*******************************')
  37.         four_day_forecast =input('是否要显示未来四天天气?')
  38.         if four_day_forecast == '是' or 'Y' or 'y' or 'Yes' or 'yes':
  39.             for i in range(1,5):
  40.                 print('当前日期:',forecast[i].get('date'))
  41.                 print('风向(风吹来的方向):',forecast[i].get('fengxiang'))
  42.                 print('风级(无需理会代码,只看“数字+‘级’”):',forecast[i].get('fengli'))
  43.                 print('最高温度(无需理会“高温”二字):',forecast[i].get('high'))
  44.                 print('最低温度(无需理会“低温”二字):',forecast[i].get('low'))
  45.                 print('大致天气:',forecast[i].get('type'))
  46.                 print('--------------------------')
  47.     print('***********************************')

  48. show_weather(get_weather_data())
复制代码

或许你可以试一下这个经过修改的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-2-11 22:09:07 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-1 22:50:01 | 显示全部楼层
两段都写得很不错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-19 22:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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