现在的位置: 首页 > 综合 > 正文

天气预报查询

2018年04月05日 ⁄ 综合 ⁄ 共 3388字 ⁄ 字号 评论关闭

天气预报查询时目前比较热门的。当然,除了天气以外,还有其他很多的查询。方法都是差不多。

无非方法有以下:

1:通过查询网页的方式,利用正则表达式来查询。特点是,使用广,可以查询的东西很多。但是由于网页编码如果有变动。那么客户端也必须变动。这个比较麻烦。

2:利用WebService来查询。现在也有很多的网站提供这种服务。这个就更加方便。但是提供的资源相对的来说没有上面的那种多。

 

下面举几个常见的例子:

Google Weather Forecast API:

 

Google Weather API 只支持美国地区使用邮政编码进行查询,例如:
http://www.google.com/ig/api?hl=zh-cn&weather=94043
(94043 为 山景城, 美国加州 的邮政编码)

而除了美国以外的地区需要使用经纬度坐标作为参数才能执行 Google Weather API, 例如:
http://www.google.com/ig/api?hl=zh-cn&weather=,,,30670000,104019996
(30670000,104019996 为 成都, 中国大陆 的经纬度坐标)

当然,也可能通行城市名称的汉语拼音来查询,例如:以下是北京的天气
http://www.google.com/ig/api?hl=zh-cn&weather=Beijing

要其它地区的经纬度坐标,可以通过 Google API 提供的国家代码列表及相应的城市经纬度坐标列表可以查询到,以下是 Google API 提供的查询参数:
http://www.google.com/ig/countries?output=xml&hl=zh-cn

(查询 Google 所支持的所有国家的代码,并以 zh-cn 简体中文显示)
http://www.google.com/ig/cities?output=xml&hl=zh-cn&country=cn

 

 

 

 

 

(1)Google API:

http://www.google.com/ig/api?hl=zh-cn&weather=Beij

ing,最后面的Beijing就是要获取天气的城市北京的拼音名,如果不是直辖市,可以在城市名后加上省名的拼音,如

http://www.google.com/ig/api?hl=zh-cn&weather=Linh

ai,Zhejiang是浙江临海市,如果按城市名无法获取数据,也可以座标的方式来获取,如http://www.google.com/ig
/api?hl=zh-cn&weather=,,,2
8800000,121130000也是浙江临海市的所在位置,具体的坐标查询方法,请参考Google,这里只讲实现方法。
我们以北京为例,把上面查询北京市天气的URL复制到浏览器中并回车,可以看到如下结果:
-<xml_api_reply version="1
-<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0
-<forecast_information <citydata="Beijing" />
<postal_codedata="Beijing" />
<latitude_e6data="" />
<longitude_e6data="" />
<forecast_datedata="2009-05-09" />
<current_date_timedata="2009-05-09 15:13:44 +0000" />
<unit_systemdata="SI" />
</forecast_information -<,武汉天气
;
current_conditions <conditiondata="晴" />
<temp_fdata="78" />
<temp_cdata="26" />
<humiditydata="湿度: 41%" />
<icondata="/ig/images/weather/sunny.png" />
<wind_conditiondata="风向:西北、风速:11 (公里/小时)" />
</current_conditions -<
forecast_conditions <day_of_weekdata="周六" />
<lowdata="11" />
<highdata="21" />
<icondata="/ig/images/weather/chance_of_rain.png" />
<conditiondata="可能有雨" />
</forecast_conditions -<
forecast_conditions <day_of_weekdata="周日" />
<lowdata="13" />
<highdata="22" />
<icondata="/ig/images/weather/mostly_sunny.png" />
<conditiondata="以晴为主" />
</forecast_conditions -<
forecast_conditions <day_of_weekdata="周一" />
<lowdata="16" />
<highdata="29" />
<,西安天气
;icondata="/ig/images/weather/mostly_sunny.png" />
<conditiondata="以晴为主" />
</forecast_conditions -<
forecast_conditions <day_of_weekdata="周二" />
<lowdata="13" />
<highdata="26" />,成都天气
;
<icondata="/ig/images/weather/mostly_sunny.png" />
<conditiondata="以晴为主" />
</forecast_conditions </weather </xml_api_reply 这是一个XML文档,天气数据就在里面,我们只需要从其中提取有用的数据再显示出来就行了。

 

 

2: Yahoo API 做天气预报

 

在Yahoo的Developer Network

http://developer.yahoo.com/weather/

详细地介绍了Yahoo天气预报的API调用方法

 

一个简单的方法来显示

 

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();
        }
    }
}

 

 

 

 

 

 

抱歉!评论已关闭.