在Yahoo的Developer Network
http://developer.yahoo.com/weather/
详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文仅作为抛砖,其它的应用由网友们自由发挥
首先了解Yahoo Weather Api的RSS Response格式:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Tangshan, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html</link>
<description>Yahoo! Weather for Tangshan, CH</description>
<language>en-us</language>
<lastBuildDate>Fri, 22 Aug 2008 8:00 am CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Tangshan" region="" country="CH"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="67" direction="290" speed="4" />
<yweather:atmosphere humidity="95" visibility="4.97" pressure="" rising="0" />
<yweather:astronomy sunrise="5:26 am" sunset="6:54 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Tangshan, CH at 8:00 am CST</title>
<geo:lat>39.63</geo:lat>
<geo:long>118.17</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html</link>
<pubDate>Fri, 22 Aug 2008 8:00 am CST</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="67" date="Fri, 22 Aug 2008 8:00 am CST" />
<description>
<![CDATA[
<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 67 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Partly Cloudy. High: 86 Low: 69<br />
Sat - Partly Cloudy. High: 88 Low: 70<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Tangshan__CH/*http://weather.yahoo.com/forecast/CHXX0131_f.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]>
</description>
<yweather:forecast day="Fri" date="22 Aug 2008" low="69" high="86" text="Partly Cloudy" code="30" />
<yweather:forecast day="Sat" date="23 Aug 2008" low="70" high="88" text="Partly Cloudy" code="30" />
<guid isPermaLink="false">CHXX0131_2008_08_22_8_00_CST</guid>
</item>
</channel>
</rss>
我们所需要用到的Node是//rss/channel/item/yweather:forecast
在这里我们用XmlDocument来实现,
using System;
using System.Xml;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
XmlDocument document = new XmlDocument();
document.Load("http://xml.weather.yahoo.com/forecastrss?p=CHXX0131");
XmlNodeList nodes = document.GetElementsByTagName("forecast",
@"http://xml.weather.yahoo.com/ns/rss/1.0");
foreach (XmlNode node in nodes)
{
Console.WriteLine("日期:{0},星期:{1},天气:{2},温度:{3}°C 至 {4}°C",
node.Attributes["date"].InnerText,
node.Attributes["day"].InnerText,
node.Attributes["text"].InnerText,
FToC(int.Parse(node.Attributes["low"].InnerText)),
FToC(int.Parse(node.Attributes["high"].InnerText)));
}
}
private static string FToC(int f)
{
return Math.Round((f - 32) / 1.8,1).ToString();
}
}
}
简单的实现了天气预报的功能了,这里调用的是河北唐山的天气,需要其它地区的天气可以这里查找代码