惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
T
Tor Project blog
T
Threat Research - Cisco Blogs
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Register - Security
The Register - Security
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Hacker News
The Hacker News
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
T
Tenable Blog
H
Hacker News: Front Page
B
Blog
宝玉的分享
宝玉的分享
C
Check Point Blog
美团技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
G
GRAHAM CLULEY
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Scott Helme
Scott Helme
L
Lohrmann on Cybersecurity
量子位
A
About on SuperTechFans
V2EX - 技术
V2EX - 技术
T
The Exploit Database - CXSecurity.com

博客园 - hzman

ASP采集入库生成本地文件的几个FUCTION 用XMLhttp编写web采集程序 无需安装软件架设NOD32升级服务器指南! 架设NOD32升级服务器 - hzman - 博客园 C#游戏外挂代码 VC7转换到VC8的常见问题 ini文件操作类 OllyDBG 入门系列(二)-字串参考 OllyDBG 入门系列(一)-认识OllyDBG - hzman - 博客园 live.cn邮箱注册帐号方法 - hzman - 博客园 免手机验证码网站备案 ASP同一虚拟空间放多个站点(各站点对应独立域名)的方法 QQ空间代码说明 页面间传值方法 C#代码,功能为拦截http请求,并篡改其中任何内容 C#编写NotifyIcon 深入浅出之正则表达式 提取HTML代码中文字的C#函数 c#字符串截取函数
两个分析HTML网页的方法
hzman · 2007-06-20 · via 博客园 - hzman

有人想把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中用正则表达式来分析。这个方法可以用在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");
   }
  }
 }
}

真正做爬虫的,都是用正则表达式来做抽取的,可以找些开源的爬虫,代码都差不多。只是有些更高,可以把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”赫然在列。