使用新浪天气预报接口获取天气预报信息

关键词:天气预报 新浪天气预报接口 Json数据格式 天气预报 Asp.NET C#

  新浪天气预报查询的页面结构再次更改,郁闷,又得重新修改正则表达式。返回的结果是json格式的字符串,结构如下
+展开
-JavaScript
var weather={success:true,addr:'桂林',weathers:[{d:'2010-09-26',weather:'小雨转阵雨',tmp:'27℃~20℃',dir:'北风',strong:'≤3级'},{d:'2010-09-27',weather:'多云',tmp:'30℃ ~ 21℃',dir:'北风',strong:'≤3级'},{d:'2010-09-28',weather:'多云',tmp:'30℃ ~ 21℃',dir:'北风',strong:'≤3级'},{d:'2010-09-29',weather:'多云',tmp:'30℃ ~ 21℃',dir:'北风',strong:'≤3级'},{d:'2010-09-30',weather:'多云',tmp:'30℃',dir:'北风',strong:'≤3级'}]}


  使用者可以使用script标签来加载动态页得到对应城市的天气预报信息,然后根据自己的网站风格生成天气预报的显示格式。

  源代码如下 weather.ashx
+展开
-C#
<%@ WebHandler Language="C#" Class="weather" %>

using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
public class weather : IHttpHandler {
    public void ProcessRequest(HttpContext context)
    {
        string addr = context.Request["addr"], json = "{success:false}";
        if (!string.IsNullOrEmpty(addr))
        {
            try
            {
                HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" + addr);
                string htmlBody = "";
                using (StreamReader sr = new StreamReader(wr.GetResponse().GetResponseStream(), Encoding.GetEncoding(936)))
                {
                    htmlBody = sr.ReadToEnd();
                }                
                //javascript:sent_to_vb('桂林','桂林今日(2010年09月26日)天气:小雨转阵雨,27℃~20℃,风力:≤3级,风向:北风');  ==>今天
                Regex r = new Regex(@"sent_to_vb\('" + addr.ToString() + @"','([^']+)'\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (r.IsMatch(htmlBody))
                {
                    DateTime dt = DateTime.Now;
                    Match m = r.Match(htmlBody);
                    string[] tmpArr = m.Groups[1].Value.Split(',');
                    json = "{success:true,addr:'" + Format.Js(addr) + "',weathers:[{d:'" + dt.ToString("yyyy-MM-dd") + "'"
                            + ",weather:'" + tmpArr[0].Split(':')[1] + "',tmp:'" + tmpArr[1] + "',dir:'" + tmpArr[3].Split(':')[1] + "',strong:'" + tmpArr[2].Split(':')[1] + "'}";
                    Regex dsrx = new Regex("
[\\s\\S]+?
    ([\\s\\S]+?)
", RegexOptions.IgnoreCase | RegexOptions.Compiled)
                        , lirx = new Regex("]*>([\\s\\S]*?)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (dsrx.IsMatch(htmlBody))
                    {
                        MatchCollection mc = dsrx.Matches(htmlBody), mcliDay, mcliNight;
                        string dweather = "", nweather = "", dtmp = "", ntmp = "", ddir = "", ndir = "", dstrong = "", nstrong = "";
                        for (int i = 0; i < mc.Count; i+=2)
                        {
                            mcliDay = lirx.Matches(mc[i].Groups[1].Value);
                            mcliNight = lirx.Matches(mc[i + 1].Groups[1].Value);
                            
                            dweather = mcliDay[0].Groups[1].Value; nweather = mcliNight[0].Groups[1].Value; if (nweather != "" && nweather != "暂无" && nweather != dweather) nweather = " 转 " + nweather; else nweather = "";
                            dtmp = mcliDay[1].Groups[1].Value; ntmp = mcliNight[1].Groups[1].Value; if (ntmp != "" && ntmp != dtmp) ntmp = " ~ " + ntmp; else ntmp = "";
                            ddir = mcliDay[2].Groups[1].Value; ndir = mcliNight[2].Groups[1].Value; if (ndir != "" && ndir != ddir) ndir = " 转 " + ndir; else ndir = "";
                            dstrong = mcliDay[3].Groups[1].Value; nstrong = mcliNight[3].Groups[1].Value; if (nstrong != "" && nstrong != dstrong) nstrong = " 转 " + nstrong; else nstrong = "";
                            dt=dt.AddDays(1);
                            json += ",{d:'" + dt.ToString("yyyy-MM-dd") + "',weather:'" + dweather + nweather + "',tmp:'" + dtmp + ntmp + "',dir:'" + ddir + ndir + "',strong:'" + dstrong + nstrong + "'}";
                        }
                    }
                    json += "]}";
                }
            }
            catch { json = "{success:false}"; }
        }

        context.Response.Write(json);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}


  上面只是根据传递的城市来获取天气预报信息,如果是根据访问者来获取对应的天气预报信息,可以查看下面的文章。

注意:下面的这些文章都是以前发的,对应以前新浪的结构,而不是最新的,所以得自己对照本文来进行修改正则表达式和相关的代码结构。对于其他版本的代码,如asp,php,以后不再更新,只更新asp.net版本的代码,自己根据asp.net版本的代码对照修改正则和结构而已。

相关下载
根据访问用户IP地址自动获取天气预报
简易天气预报查询

相关文章
asp根据访问者ip地址获取天气预报信息
php根据访问者ip地址获取天气预报信息
获取天气预报代码修改
根据访问用户IP地址自动获取天气预报
简易天气预报查询


加支付宝好友偷能量挖...


原创文章,转载请注明出处:使用新浪天气预报接口获取天气预报信息

评论(0)Web开发网
阅读(1121)喜欢(0)Asp.Net/C#/WCF