谁有查询天气的哪个源代码发一下 谢谢
谁有查询天气的哪个源代码发一下谢谢 import urllib.requestimport gzip
import json
print('------天气查询------')
def get_weather_data() :
city_name = input('请输入要查询的城市名称:')
url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
#网址1只需要输入城市名,网址2需要输入城市代码
#print(url1)
weather_data = urllib.request.urlopen(url1).read()
#读取网页数据
weather_data = gzip.decompress(weather_data).decode('utf-8')
#解压网页数据
weather_dict = json.loads(weather_data)
#将json数据转换为dict数据
return weather_dict
def show_weather(weather_data):
weather_dict = weather_data
#将json数据转换为dict数据
if weather_dict.get('desc') == 'invilad-citykey':
print('你输入的城市名有误,或者天气中心未收录你所在城市')
elif weather_dict.get('desc') =='OK':
forecast = weather_dict.get('data').get('forecast')
print('城市:',weather_dict.get('data').get('city'))
print('温度:',weather_dict.get('data').get('wendu')+'℃ ')
print('感冒:',weather_dict.get('data').get('ganmao'))
print('风向:',forecast.get('fengxiang'))
print('风级:',forecast.get('fengli'))
print('高温:',forecast.get('high'))
print('低温:',forecast.get('low'))
print('天气:',forecast.get('type'))
print('日期:',forecast.get('date'))
print('*******************************')
four_day_forecast =input('是否要显示未来四天天气,是/否:')
if four_day_forecast == '是' or 'Y' or 'y':
for i in range(1,5):
print('日期:',forecast.get('date'))
print('风向:',forecast.get('fengxiang'))
print('风级:',forecast.get('fengli'))
print('高温:',forecast.get('high'))
print('低温:',forecast.get('low'))
print('天气:',forecast.get('type'))
print('--------------------------')
print('***********************************')
show_weather(get_weather_data())
import urllib.request
import gzip
import json
print('------天气查询------')
def get_weather_data() :
city_name = input('请输入要查询的城市名称(直接敲回车则默认为龙川):')
if city_name == '':
city_name = '龙川'
url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
#网址1只需要输入城市名,网址2需要输入城市代码
#print(url1)
weather_data = urllib.request.urlopen(url1).read()
#读取网页数据
weather_data = gzip.decompress(weather_data).decode('utf-8')
#解压网页数据
weather_dict = json.loads(weather_data)
#将json数据转换为dict数据
return weather_dict
def show_weather(weather_data):
weather_dict = weather_data
#将json数据转换为dict数据
if weather_dict.get('desc') == 'invilad-citykey':
print('您输入的城市名有误,或者天气中心暂未收录您所在城市!')
elif weather_dict.get('desc') =='OK':
forecast = weather_dict.get('data').get('forecast')
print('您输入的城市:',weather_dict.get('data').get('city'))
print('即时或大致温度:',weather_dict.get('data').get('wendu')+'℃ ')
print('感冒概率:',weather_dict.get('data').get('ganmao'))
print('风向(风吹来的方向):',forecast.get('fengxiang'))
print('风级(无需理会代码,只看“数字+‘级’”):',forecast.get('fengli'))
print('最高温度(无需理会“高温”二字):',forecast.get('high'))
print('最低温度(无需理会“低温”二字):',forecast.get('low'))
print('现在或今日大致天气:',forecast.get('type'))
print('当前日期:',forecast.get('date'))
print('*******************************')
four_day_forecast =input('是否要显示未来四天天气?')
if four_day_forecast == '是' or 'Y' or 'y' or 'Yes' or 'yes':
for i in range(1,5):
print('当前日期:',forecast.get('date'))
print('风向(风吹来的方向):',forecast.get('fengxiang'))
print('风级(无需理会代码,只看“数字+‘级’”):',forecast.get('fengli'))
print('最高温度(无需理会“高温”二字):',forecast.get('high'))
print('最低温度(无需理会“低温”二字):',forecast.get('low'))
print('大致天气:',forecast.get('type'))
print('--------------------------')
print('***********************************')
show_weather(get_weather_data())
或许你可以试一下这个经过修改的。 新手·ing 发表于 2019-1-25 20:04
谢谢 两段都写得很不错
页:
[1]