两个分析HTML网页的方法

两个分析HTML网页的方法


作者:mvm 来源:博客堂
有人想把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() &;amp;&;amp; 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中用正则表达式来分析。这个方法可以用在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*(&;amp;\\w*=\\w*)*)?[\\\"\\\']";
  Regex re=new Regex(regex);
  MatchCollection matches=re.Matches(content);
 
  System.Collections.IEnumerator enu=matches.GetEnumerator();
  while(enu.MoveNext() &;amp;&;amp; enu.Current!=null)
  {
    Match match=(Match)(enu.Current);
    Console.Write(match.Value "\r\n");
  }
  }
}


真正做爬虫的,都是用正则表达式来做抽取的,可以找些开源的爬虫,代码都差不多。只是有些更高,可以把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.tagName " " document.forms[0].childNodes.value;
  }
}
</SCRIPT> 


点了“Click Me”以后,打印出来的forms[0]的子元素列表里面,“button 1”赫然在列。    (2005-7-04:05:20)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [RegExp] 的这些文章也感兴趣:

正则表达式详解  常用正则表达式
正则表达式  用正则表达式解析C#文件
深入浅出之正则表达式(一)  正则表达式在UBB论坛中的应用
如何过滤掉html字符(c#)  RegularExpressionstoremoveorreplace
程序员必须掌握的基本正则表达式  someoftheregularexpressionslistedinthePerlCookBook
提取HTML代码中文字的C#函数  寻找匹配的Groups的几种方法
转贴:不得不看的正则表达式详解  最专业的正则表达式网站regexlib.com使用说明
两个分析HTML网页的方法  解读C#中的正则表达式
深入浅出之正则表达式(二)  正则表达式使用详解及实例
几个常用的用正则表达式验证字符串的函数  C#正则表达式应用范例