Python # 和风天气接口

###   备注只是好奇测试了下。

    和风天气官网地址:https://www.heweather.com/

    和风天气接口注册后可以免费试用。

 服务器节点接口地址
付费用户中国、北美、欧洲、东南亚https://api.heweather.com/v5/
免费用户中国https://free-api.heweather.com/v5/
# -*- coding: utf-8 -*-
import urllib2,json
#调用和风天气的API city可以通过https://cdn.heweather.com/china-city-list.txt城市列表获取
url = 'https://free-api.heweather.com/v5/weather?city=CN101230201&key=8a439a7e0e034cdcb4122c918f55e5f3'
#用urllib2创建一个请求并得到返回结果
req = urllib2.Request(url)
resp = urllib2.urlopen(req).read()
# print resp
# print type(resp)

#将JSON转化为Python的数据结构
json_data = json.loads(resp)
city_data=json_data['HeWeather5'][0]
hourly_data= json_data['HeWeather5'][0]['hourly_forecast']
daily_data = json_data['HeWeather5'][0]['daily_forecast']
print json_data
print u'当前时间:' + daily_data[0]['date']
print u'城市:' + city_data['basic']['city']
print u'PM指数:' + city_data['aqi']['city']['pm25']
print u'白天天气:' + daily_data[0]['cond']['txt_d']
print u'夜间天气:' + daily_data[0]['cond']['txt_n']
print u'今天{0}: 气温:{1}°/{2}°'.format(str(daily_data[0]['date']),daily_data[0]['tmp']['min'],daily_data[0]['tmp']['max'])
print u'未来小时天气:{0} {1}'.format(str(hourly_data[0]['date']).split()[1],hourly_data[0]['cond']['txt'])
print u'未来小时天气:{0} {1}'.format(str(hourly_data[1]['date']).split()[1],hourly_data[1]['cond']['txt'])
print u'未来小时天气:{0} {1}'.format(str(hourly_data[2]['date']).split()[1],hourly_data[2]['cond']['txt'])
print u'未来{0} 天气:{1}°/{2}°'.format(daily_data[1]['date'],daily_data[1]['tmp']['min'],daily_data[1]['tmp']['max'])
print u'未来{0} 天气:{1}°/{2}°'.format(daily_data[2]['date'],daily_data[1]['tmp']['min'],daily_data[2]['tmp']['max'])
print u'穿衣建议:' + json_data['HeWeather5'][0]['suggestion']['drsg']['txt']

 

###

转载于:https://www.cnblogs.com/lwsup/p/7535670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值