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
    }
}

转载于:https://www.cnblogs.com/yuji/archive/2009/05/05/1449318.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值