在Yahoo的Developer Network
http://developer.yahoo.com/weather/
详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文仅作为抛砖,其它的应用由网友们自由发挥
在Yahoo的Developer Network
http://developer.yahoo.com/weather/
详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文仅作为抛砖,其它的应用由网友们自由发挥
首先了解Yahoo Weather Api的RSS Response格式:

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来实现,

Console Codes
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();
}
}
}
简单的实现了天气预报的功能了,这里调用的是河北唐山的天气,需要其它地区的天气可以这里查找代码
http://weather.yahoo.com/China/CHXX/regional.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述