码迷,mamicode.com
首页 > Windows程序 > 详细

雅虎天气API使用笔记

时间:2015-05-28 16:10:30      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

查询时base_url:
https://query.yahooapis.com/v1/public/yql?q= , 公共查询接口,有限制查询次数
https://query.yahooapis.com/v1/yql?q= , 注册后获取ApkId,有限制查询次数但是已经足够使用了,10万次/天


第一步,手机端定位用户的位置(根据IP查询,或者。。。。),得到用户所在城市名称,或者城市名称的拼音,或者经纬度

第二步,根据用户所在城市的中文、英文、经纬度,获取woeid:


1,这里text键值可以用 中文、英文、经纬度。如


1): select * from geo.placefinder where text="Guangzhou"


2): select * from geo.placefinder where text="北京"


3): select * from geo.placefinder where text="39.9919336,116.3404132" and gflags = "R"


2,使用如下url查询城市的woeid:三种方法均可使用
https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="39.9919336,116.3404132" and gflags = "R"&format=json
https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="北京"&format=json
https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="Guangzhou"&format=json


注意使用经纬度获取woeid时一直取不到,后来才知道时缺少一个参数 fglags参数。,不许加 and gflags = "R"这一串才行


返回的xml格式的数据:
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2015-05-28T03:28:29Z" yahoo:lang="en-US">
<results>
<Result>
<quality>40</quality>
<latitude>23.12911</latitude>
<longitude>113.26336</longitude>
<offsetlat>23.12911</offsetlat>
<offsetlon>113.26336</offsetlon>
<radius>17700</radius>
<name/>
<line1/>
<line2>Guangzhou</line2>
<line3>Guangdong</line3>
<line4>People‘s Republic of China</line4>
<house/>
<street/>
<xstreet/>
<unittype/>
<unit/>
<postal/>
<neighborhood/>
<city>Guangzhou</city>
<county/>
<state>Guangdong</state>
<country>People‘s Republic of China</country>
<countrycode>CN</countrycode>
<statecode>44</statecode>
<countycode/>
<hash/>
<woeid>2161838</woeid> ,这个就是需要的woeid
<woetype>7</woetype>
</Result>
</results>
</query>
<!--  total: 11  -->






第三步,根据上面返回的结果中得到woeid,查询天气数据:
1):把第一步获取的woeid放到下面的查询语句中:
select * from weather.forecast where woeid=2502265 and u="c"


2):最后使用如下url即可得到天气数据:
https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid=2502265 and u="c"&format=json


返回的xml格式的数据:


<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2015-05-28T03:24:54Z" yahoo:lang="en-US">
<results>
<channel>
<title>Yahoo! Weather - Sunnyvale, CA</title>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_c.html
</link>
<description>Yahoo! Weather for Sunnyvale, CA</description>
<language>en-us</language>
<lastBuildDate>Wed, 27 May 2015 7:55 pm PDT</lastBuildDate>
<ttl>60</ttl>
<yweather:location xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" city="Sunnyvale" country="United States" region="CA"/>
<yweather:units xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" distance="km" pressure="mb" speed="km/h" temperature="C"/>
<yweather:wind xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" chill="15" direction="350" speed="19.31"/>
<yweather:atmosphere xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" humidity="78" pressure="1016.7" rising="1" visibility="16.09"/>
<yweather:astronomy xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" sunrise="5:50 am" sunset="8:19 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
</url>
</image>
<item>
<title>Conditions for Sunnyvale, CA at 7:55 pm PDT</title>
<geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">37.37</geo:lat>
<geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">-122.04</geo:long>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_c.html
</link>
<pubDate>Wed, 27 May 2015 7:55 pm PDT</pubDate>
<yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="34" date="Wed, 27 May 2015 7:55 pm PDT" temp="15" text="Fair"/>
<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br /> <b>Current Conditions:</b><br /> Fair, 15 C<BR /> <BR /><b>Forecast:</b><BR /> Wed - Partly Cloudy. High: 21 Low: 12<br /> Thu - AM Clouds/PM Sun. High: 23 Low: 12<br /> Fri - AM Clouds/PM Sun. High: 24 Low: 13<br /> Sat - AM Clouds/PM Sun. High: 24 Low: 13<br /> Sun - AM Clouds/PM Sun. High: 22 Low: 14<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]>
</description>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="29" date="27 May 2015" day="Wed" high="21" low="12" text="Partly Cloudy"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="28 May 2015" day="Thu" high="23" low="12" text="AM Clouds/PM Sun"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="29 May 2015" day="Fri" high="24" low="13" text="AM Clouds/PM Sun"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="30 May 2015" day="Sat" high="24" low="13" text="AM Clouds/PM Sun"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="31 May 2015" day="Sun" high="22" low="14" text="AM Clouds/PM Sun"/>
<guid isPermaLink="false">USCA1116_2015_05_31_7_00_PDT</guid>
</item>
</channel>
</results>
</query>
<!--  total: 22  -->




注:
url后面的参数为设置返回数据类型,json或xml


这样就能拿到以摄氏度返回的天气了。
这里注意一点,天气的返回值里面有一个code,那个代表的是各种天气类型,暴风雨啊龙卷风啊冰雹啊之类的。






code与其对应天气状态:


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
3200 not available

雅虎天气API使用笔记

标签:

原文地址:http://blog.csdn.net/u014289186/article/details/46124461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!