QQ天气预报分析

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net;
using System.IO;

namespace ArticleRelease
{
      /// <summary>
      /// WebForm1 的摘要说明。
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
  
           private void Page_Load(object sender, System.EventArgs e)
           {
                WebRequest wreq=WebRequest.Create
                    ("http://weather.news.qq.com/inc/ss84.htm");
                HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();
                     
                string HTML ="";
                Stream s=wresp.GetResponseStream();

                //StreamReader objReader = new StreamReader(s);
                StreamReader objReader = new StreamReader
                    (s,System.Text.Encoding.GetEncoding("gb2312"));

                string sLine = "";
                int i = 0;

                while (sLine!=null)
                {
                        i++;
                        sLine = objReader.ReadLine();
                        if (sLine!=null)
                        HTML += sLine; 
                 }

                  //Response.Write(HTML);

                  //根据源文件分析,源文件如有改动,必须修改.
                  string city_templete = "<td height=\"23\" width=\"72\" align=\"center\" background=\"/images/r_tembg4.gif\" class=\"rb12\">";
                  string weather_templete = "<td height=\"23\" width=\"117\" background=\"/images/r_tembg5.gif\" align=\"center\">";
                  string others_templete ="57\" height=\"13\"></td><td height=\"23\" align=\"center\">";
                  System.Text.RegularExpressions.Regex regex_city = 
                      new  System.Text.RegularExpressions.Regex(city_templete);
                  System.Text.RegularExpressions.Regex regex_weather =
                      new System.Text.RegularExpressions.Regex(weather_templete);
                   System.Text.RegularExpressions.Regex regex_others =
                       new System.Text.RegularExpressions.Regex(others_templete);
                   System.Text.RegularExpressions.Match match_city =
                       regex_city.Match(HTML);
                    System.Text.RegularExpressions.Match match_weather =
                        regex_weather.Match(HTML);
                    System.Text.RegularExpressions.MatchCollection match_others = 
                        regex_others.Matches(HTML);
                    string str;

                    if (match_city.Success)
              {
                //Response.Write("Found match_city at position " +
                         // match_city.Index +"<br>"); //输入匹配字符的位置
                         str = HTML.Substring(match_city.Index+city_templete.Length);
                          str = str.Substring(0,str.IndexOf("</td>"));
                          Response.Write("城市 : " + str + "<br>");

               }
                     else
                     {
                          throw new ArgumentException("QQ天气预报源文件修改!");
                      }

                     if (match_weather.Success)
                     {
                          //Response.Write("Found match_weather at position " 
                          //match_weather.Index + "<br>"); //输入匹配字符的位置
                            str = HTML.Substring(match_weather.Index+weather_templete.Length);
                            str = str.Substring(0,str.IndexOf("</td>"));
                            Response.Write("状况 : " + str + "<br>");
                       }
                       else
                      {
                           throw new ArgumentException("QQ天气预报源文件修改!");
                      }

                      if (match_others.Count == 4)
                      {
                           for(int j=0;j<4;j++)
                           {
                                    //Response.Write("Found match_weather at position "
                                    //+match_others[j].Index + "<br>"); //输入匹配字符的位置
                                    str = HTML.Substring(match_others[j].Index
                                        +others_templete.Length);
                                     str = str.Substring(0,str.IndexOf("</td>"));
                                     switch(j)
                                     {
                                          case 0:
                                              Response.Write("温度 : " + str + "<br>");
                                              break;
                                           case 1:
                                               Response.Write("风力 : " + str + "<br>");
                                               break;
                                            case 2:
                                                 Response.Write("紫外线 : " + str + "<br>");
                                                 break;
                                             case 3:
                                                   Response.Write("风力 : " + str +"<br>");
                                                   break;
                                        }

                                    }
                                }
                                else
                                {
                                     throw new ArgumentException("QQ天气预报源文件修改!");
                                 }


        }

       #region Web 窗体设计器生成的代码
       override protected void OnInit(EventArgs e)
       {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
             base.OnInit(e);
       }
   
     /// <summary>
     /// 设计器支持所需的方法 - 不要使用代码编辑器修改
     /// 此方法的内容。
     /// </summary>
    private void InitializeComponent()
    {   
            this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
}

你可能感兴趣的:(天气预报)