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千人口的小镇
- 免费
缺点:
- 访问速度一般
- 不是所有城市都有完整的信息。什么意思?就是有些小地方(就是人口只有一千的那种国外小城)不会像大城市那样有完整的风速、能见度、未来9天的天气预报,小地方可能只有未来三天的预报甚至只有当前的天气情况
- 天气信息较少,没有什么PM2.5、人体舒适度这种信息
这个接口返回的信息:
主要会使用的信息:
- 日出、日落时间
- 当前湿度、气压、能见度、天气情况、气温
- 未来9天的天气预报(天气情况、最高、最低温度)
- 更新时间
这里面有个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语句的创建,下面的文章有详细的介绍:
我个人推荐使用指定woeid的查询语句
select * from weather.forecast where woeid=25