C# Spider程序分析HTML網(wǎng)頁(yè)的方法

2010-08-28 10:49:27來(lái)源:西部e網(wǎng)作者:

有人想把Web Page拉下來(lái)并抽取其中的內(nèi)容。這其實(shí)是搜索引擎的一項(xiàng)最最基本的工作:下載,抽取,再下載。我早年做過(guò)一個(gè)Search Engine項(xiàng)目,不過(guò)代碼都已經(jīng)不見(jiàn)了。這次有人又問(wèn)到我這個(gè)事情,我給攢了兩個(gè)方法。

方法A,在一個(gè)WinForm里面用一個(gè)隱藏的Browser控件下載Web Page,并用IHTMLDocument來(lái)分析內(nèi)容。這個(gè)方法比較簡(jiǎn)單,但如果對(duì)于大量文件的分析速度很慢。

這個(gè)方法中用到的主要代碼如下:
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中用正則表達(dá)式來(lái)分析。這個(gè)方法可以用在Web Crawler等需要分析很多Web Page的應(yīng)用中。

下面是一個(gè)例子,能夠把http://www.google.com首頁(yè)里的所有的Hyperlink都抽取出來(lái):

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");
   }
  }
 }
}

真正做爬蟲(chóng)的,都是用正則表達(dá)式來(lái)做抽取的,可以找些開(kāi)源的爬蟲(chóng),代碼都差不多。只是有些更高,可以把Flash或者JavaScript里面的URL都抽取出來(lái)。

再補(bǔ)充一個(gè),有人問(wèn)我如果一個(gè)element是用document.write畫(huà)出來(lái)的,還能不能在DOM里面取到。答案是肯定的。具體取的方法也和平常的一樣。下面這個(gè)HTML就演示了從DOM里面取到用document.write動(dòng)態(tài)生成的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>

點(diǎn)了“Click Me”以后,打印出來(lái)的forms[0]的子元素列表里面,“button 1”赫然在列。

原文地址:http://blog.joycode.com/mvm/archive/2004/04/27/20352.aspx

關(guān)鍵詞:C#

贊助商鏈接: