兩個分析HTML網頁的方法

來源:互聯網
上載者:User
有人想把Web Page拉下來並抽取其中的內容。這其實是搜尋引擎的一項最最基本的工作:下載,抽取,再下載。我早年做過一個Search Engine項目,不過代碼都已經不見了。這次有人又問到我這個事情,我給攢了兩個方法。

方法a,在一個winform裡面用一個隱藏的browser控制項下載web Page,並用IHTMLDocument來分析內容。這個方法比較簡單,但如果對於大量檔案的分析速度很慢。

這個方法中用到的主要代碼如下:

private void button1_Click(object sender, System.EventArgs e) {
 object url="http://www.google.com";
 object nothing=null;
 this.axWebBrowser1.Navigate2(ref url,ref nothing,ref nothing,ref nothing,ref nothing);
 this.axWebBrowser1.DownloadComplete+=new System.EventHandler(this.button2_Click);
}

private void button2_Click(object sender, System.EventArgs e) {
 this.textBox1.Text="";
 mshtml.IHTMLDocument2 doc=(mshtml.IHTMLDocument2)this.axWebBrowser1.Document;
 mshtml.IHTMLElementCollection all=doc.all;
 System.Collections.IEnumerator enumerator=all.GetEnumerator();
 while(enumerator.MoveNext() && enumerator.Current!=null)
 {
  mshtml.IHTMLElement element=(mshtml.IHTMLElement)(enumerator.Current);
  if(this.checkBox1.Checked==true)
  {
   this.textBox1.Text+="\r\n\r\n"+element.innerHTML;
  }
  else
  {
   this.textBox1.Text+="\r\n\r\n"+element.outerHTML;
  }
 }
}

方法b,用system.net.webclient下載web Page存到本地檔案或者String中用Regex來分析。這個方法可以用在Web Crawler等需要分析很多Web Page的應用中。

下面是一個例子,能夠把http://www.google.com首頁裡的所有的Hyperlink都抽取出來:

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace HttpGet{
 class Class1{
  [STAThread]
  static void Main(string[] args){
   System.Net.WebClient client=new WebClient();
   byte[] page=client.DownloadData("http://www.google.com");
   string content=System.Text.Encoding.UTF8.GetString(page);
   string regex="href=[\\\"\\\'](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\']";
   Regex re=new Regex(regex);
   MatchCollection matches=re.Matches(content);
  
   System.Collections.IEnumerator enu=matches.GetEnumerator();
   while(enu.MoveNext() && enu.Current!=null)
   {
    Match match=(Match)(enu.Current);
    Console.Write(match.Value+"\r\n");
   }
  }
 }
}

真正做爬蟲的,都是用Regex來做抽取的,可以找些開源的爬蟲,代碼都差不多。只是有些更高,可以把flash或者javascript裡面的url都抽取出來。

再補充一個,有人問我如果一個element是用document.write畫出來的,還能不能在dom裡面取到。答案是肯定的。具體取的方法也和平常的一樣。下面這個html就示範了從dom裡面取到用document.write動態產生的html Tag:

<form>
<SCRIPT>
   document.write("<input type=button id='btn1' value='button 1'>");
</SCRIPT>
<INPUT onclick=show() type=button value="click me">
</FORM>
<TEXTAREA id=allnode rows=29 cols=53></TEXTAREA>
<SCRIPT>
function show()
{
   document.all.item("allnode").innerText="";
   var i=0;
   for(i=0;i<document.forms[0].childNodes.length;i++)
   {
      document.all.item("allnode").innerText=document.all.item("allnode").innerText+"\r\n"+document.forms[0].childNodes[i].tagName+" "+document.forms[0].childNodes[i].value;
   }
}
</SCRIPT>

點了“Click Me”以後,列印出來的forms[0]的子項目列表裡面,“button 1”赫然在列。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.