简易天气预报查询

一个简单的天气预报查询,可以查询当前及后两天的数据。
天气预报数据是从sina网爬下来的,所以使用了正则来获取数据。如果sina改版过的话,需要修改对应的正则表达式。

完整示例下载点击这里
简易天气预报查询

返回的数据为json格式的,大概如下
+展开
-JavaScript
{
  success:true//只是是否成功
  ,weathers:[//天气数组,从今天到后两天
    {d:'日期',weather:'天气',tmp:'温度',dir:'风向',strong:'风力'}//注意只有当前才有“风向”属性
    ,{d:'日期',weather:'天气',tmp:'温度',strong:'风力'}//明天
    ,{d:'日期',weather:'天气',tmp:'温度',strong:'风力'}//后天
  ]
}




下面贴出爬虫的主要程序代码
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 {
   /// <summary>
    /// 移除字符串中指定的html标记,如果未传递标记名称则全部移除html标记
    /// </summary>
    /// <param name="str">字符串对象</param>
    /// <param name="Tags">标记名称</param>
    /// <returns></returns>
    public static string RemoveHTML(string str, params string[] Tags)
    {
        string Pattern = "";
        if (Tags.Length == 0) Pattern = "<[^>]+>";
        else
        {
            for (int i = 0; i < Tags.Length; i++)
            {
                Pattern += "<" + Tags[i] + "[^>]*>|</" + Tags[i] + ">|";
            }
            Pattern = Pattern.Substring(0, Pattern.Length - 1);
        }
        return Regex.Replace(str, Pattern, "", RegexOptions.IgnoreCase);
    }
    /// <summary>
    /// 从字符串中获取天气,温度和风力
    /// </summary>
    /// <param name="str">内容字符串</param>
    /// <param name="IsToday">是否为今天</param>
    /// <returns></returns>
    private string[] Split(string str, bool IsToday)
    {
        string s1 = ""//风向,只有今天才有
            , s2 = ""//天气,非今天
            , s3 = ""//温度,非今天
            , s4 = "";//风力
        Regex r;
        Match m;
        if (IsToday)
        {
            r = new Regex("风向:([\\s\\S]+?)</li>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            m = r.Match(str);
            s1 = m.Groups[1].Value.Trim();
        }
        else
        {
            r = new Regex("天气:([\\s\\S]+?)</p>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            m = r.Match(str);
            s2 = m.Groups[1].Value.Trim();
            r = new Regex("温度:([\\s\\S]+?)</p>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            m = r.Match(str);
            s3 = RemoveHTML(m.Groups[1].Value).Trim();
        }
        r = new Regex("风力:([\\s\\S]+?)" + (IsToday ? "</li>" : "</p>"), RegexOptions.Compiled | RegexOptions.IgnoreCase);
        m = r.Match(str);
        s4 = m.Groups[1].Value.Trim();
        return new string[] { s1, s2, s3, s4 };
    }
    public void ProcessRequest(HttpContext context)
    {
        string json = "{success:false}", addr = context.Request.Form["addr"];
        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();
                }
                Regex r = new Regex("<div\\s+class=\"box-s1-l\">([\\s\\S])+?<h2>([^>]+)</h2>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (r.IsMatch(htmlBody))
                {
                    json = "{success:true,weathers:[{d:'" + DateTime.Now.ToString("yyyy-MM-dd") + "'";
                    Match m = r.Match(htmlBody);
                    json += ",weather:'" + m.Groups[2].Value + "'";
                    r = new Regex("<div\\s+class=\"w-number\">([\\s\\S]+?)</div>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    m = r.Match(htmlBody);
                    json += ",tmp:'" + RemoveHTML(m.Groups[1].Value).Trim() + "'";
                    r = new Regex("<ul\\s+class=\"list-s1\">([\\s\\S]+?)</ul>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    m = r.Match(htmlBody);
                    string[] tmp = Split(m.Groups[1].Value, true);
                    json += ",dir:'" + tmp[0] + "',strong:'" + tmp[3] + "'}";

                    r = new Regex("<div\\s+class=\"info-area\">([\\s\\S]+?)</div>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    MatchCollection mc = r.Matches(htmlBody);
                    for (int i = 1; i < 3 && i < mc.Count; i++)
                    {
                        tmp = Split(mc[i].Groups[1].Value, false);
                        json += ",{d:'" + DateTime.Now.AddDays(i).ToString("yyyy-MM-dd") + "',weather:'" + tmp[1]
                            + "',tmp:'" + tmp[2] + "',strong:'" + tmp[3] + "'}";
                    }

                    json += "]}";
                }
            }
            catch { }
        }
        context.Response.Write(json);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

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


原创文章,转载请注明出处:简易天气预报查询

评论(0)Web开发网
阅读(226)喜欢(0)JavaScript/Ajax开发技巧