2010-01-18 81 views
0

private void button3_Click(object sender,EventArgs e) { listBox1.Items.Clear();關於網頁解析

 string szURL = textBox1.Text;// "http://localhost"; 
     //textBox1.Text = szURL; 
     HttpWebRequest httpRequest; 
     HttpWebResponse httpResponse; 
     string bodyText = ""; 
     Stream responseStream; 
     Byte[] RecvBytes = new Byte[Byte.MaxValue]; 
     Int32 bytes; 
     httpRequest = (HttpWebRequest)WebRequest.Create(szURL); 
     httpResponse = (HttpWebResponse)httpRequest.GetResponse(); 
     responseStream = httpResponse.GetResponseStream(); 
     while (true) 
     { 
      bytes = responseStream.Read(RecvBytes, 
      0, RecvBytes.Length); 
      if (bytes <= 0) break; 
      bodyText += System.Text.Encoding.UTF8.GetString(RecvBytes, 
      0, bytes); 
     } 
     //listBox1.Items.Add(bodyText); 
     textBox2.Text = bodyText; 

     MatchCollection m1 = Regex.Matches(bodyText, @"(<a.*?>.*?</a>)", 
       RegexOptions.Singleline); 

     // 2. 
     // Loop over each match. 
     foreach (Match m in m1) 
     { 
      string value = m.Groups[1].Value; 
      // LinkItem i = new LinkItem(); 

      // 3. 
      // Get href attribute. 
      Match m2 = Regex.Match(value, @"<\s*script[^>]*>(?<content>.*?)<\s*/\s*\script\s*>", 
       RegexOptions.Singleline); 
      if (m2.Success) 
      { 
       listBox1.Text = m2.Groups[1].Value; 
      } 

      // 4. 
      // Remove inner tags from text. 

      string t = Regex.Replace(value, @"\s*<.*?>\s*", "", 
       RegexOptions.Singleline); 
      // i.Text = t; 
      listBox1.Items.Clear(); 
      listBox1.Items.Add(t); 

     } 




    } 

這是我的代碼,它被賦予作爲分配給我..我必須在標籤之間的內容區分開來......獨自從網頁中的鏈接...我覺得很困難。請儘快幫助我..

回答

1

解析HTML很困難,您應該嘗試使用第三方框架來構建HTML DOM(最好使用某種形式的標記器)而不是使用正則表達式。當你使用.NET時,我強烈建議你看看使用HTMLAgility Pack

它(HTML敏捷性包)是一個.NET代碼庫,使您解析「從網絡中」 HTML文件。解析器對「真實世界」格式錯誤的HTML非常寬容。對象模型與提出System.Xml非常相似,但是對於HTML文檔(或流)。

+0

謝謝你的想法 – user247958 2010-01-19 08:42:48