使用Yahoo天气接口实现全球天气预报 JavaScript+Ajax

本文介绍了因Yahoo Weather API退役,转而使用新接口https://weather-ydn-yql.media.yahoo.com/forecastrss的情况。该接口全球覆盖广泛,但并非所有城市都有完整信息。主要内容包括接口优缺点、关键信息、天气代码解释,以及如何创建YQL查询语句,特别是推荐使用WOEID查询以避免重名地名问题。
摘要由CSDN通过智能技术生成

2019年1月7日更新

Important EOL Notice: As of Thursday, Jan. 3, 2019, the weather.yahooapis.com and query.yahooapis.com for Yahoo Weather API will be retired. 
To continue using our free Yahoo Weather APIs, use https://weather-ydn-yql.media.yahoo.com/forecastrss. Contact yahoo-weather-ydn-api@oath.com for credentials to onboard to this free Yahoo Weather API service.

雅虎的天气接口已变为 https://weather-ydn-yql.media.yahoo.com/forecastrss


 

先上效果:

国内的天气预报接口对国外城市的支持不够好,国内对全球支持最好的和风天气却没有JavaScript。所以我选择了yahoo的天气接口

优点

 

  1. 城市全,全球几乎所有的城市都有,包括国外那种只有1千人口的小镇
  2. 免费

缺点

 

  1. 访问速度一般
  2. 不是所有城市都有完整的信息。什么意思?就是有些小地方(就是人口只有一千的那种国外小城)不会像大城市那样有完整的风速、能见度、未来9天的天气预报,小地方可能只有未来三天的预报甚至只有当前的天气情况
  3. 天气信息较少,没有什么PM2.5、人体舒适度这种信息

这个接口返回的信息:

主要会使用的信息:

 

  1. 日出、日落时间
  2. 当前湿度、气压、能见度、天气情况、气温
  3. 未来9天的天气预报(天气情况、最高、最低温度)
  4. 更新时间

这里面有个code是指的天气情况的代码:

var weatherCode=[
	'龙卷风',//0 tornado
	'热带风暴',//1 tropical storm
	'飓风',//2 hurricane
	'次剧烈雷雨',//3 severe thunderstorms
	'雷雨',//4 thunderstorms
	'雨夹雪',//5 mixed rain and snow
	'雨夹雪',//6 mixed rain and sleet
	'雨夹雪',//7 mixed snow and sleet
	'毛毛雨',//8 freezing drizzle
	'小雨',//9 drizzle
	'冻雨',//10 freezing rain
	'小阵雨',//11 showers
	'大阵雨',//12 showers
	'小雪花',//13 snow flurries
	'小雪阵雨',//14 light snow showers
	'风雪天',//15 blowing snow
	'雪',//16 snow
	'冰雹',//17 hail
	'雨夹雪',//18 sleet
	'灰尘',//19 dust
	'雾',//20 foggy
	'霾',//21 haze
	'烟',//22 smoky
	'风暴',//23 blustery
	'多风',//24 windy
	'冷',//25 cold
	'多云',//26 cloudy
	'多云',//27 mostly cloudy (night)
	'多云',//28 mostly cloudy (day)
	'多云转晴',//29 partly cloudy (night)
	'多云转晴',//30 partly cloudy (day)
	'晴',//31 clear (night)
	'晴',//32 sunny
	'少云',//33 fair (night)
	'少云',//34 fair (day)
	'混合雨和冰雹',//35 mixed rain and hail
	'炎热',//36 hot
	'零星雷暴',//37 isolated thunderstorms
	'零星雷阵雨',//38 scattered thunderstorms
	'零星雷阵雨',//39 scattered thunderstorms
	'零星阵雨',//40 scattered showers
	'大雪',//41 heavy snow
	'分散的阵雪',//42 scattered snow showers
	'大雪',//43 heavy snow
	'部分多云',//44 partly cloudy
	'雷阵雨',//45 thundershowers
	'阵雪',//46 snow showers
	'零星雷阵雨',//47 isolated thundershowers
	'无法获取',//48 3200 not available
];

使用这个天气接口最主要最关键的是这个YQL语句的创建,下面的文章有详细的介绍:

雅虎天气API使用笔记

我个人推荐使用指定woeid的查询语句

select * from weather.forecast where woeid=25
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值