原文 c# 正则表达式对网页进行内容抓取

搜索引擎中一个比较重要的环节就是从网页中抽取出有效内容。简单来说,就是吧HTML文本中的HTML标记去掉,留下我们用IE等浏览器打开HTML文档看到的部分(我们这里不考虑图片).

将HTML文本中的标记分为:注释,script ,style,以及其他标记分别去掉: 

1.去注释,正则为: 

output = Regex.Replace(input, @"<!--[^-]*-->", string.Empty, RegexOptions.IgnoreCase); 

2.去script,正则为: 

ouput = Regex.Replace(input, @"<script[^>]*?>.*?</script>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline); 

output2 = Regex.Replace(ouput , @"<noscript[^>]*?>.*?</noscript>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline); 

3.去style,正则为: 

output = Regex.Replace(input, @"<style[^>]*?>.*?</style>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline); 

4.去其他HTML标记 

result = result.Replace(" ", " "); 

result = result.Replace(""", "\""); 

result = result.Replace("<", "<"); 

result = result.Replace(">", ">"); 

result = result.Replace("&", "&"); 

result = result.Replace("<br>", "\r\n"); 

result = Regex.Replace(result, @"<[\s\S]*?>", string.Empty, RegexOptions.IgnoreCase); 

以上的代码中大家可以看到,我使用了RegexOptions.Singleline参数,这个参数很重要,他主要是为了让"."(小圆点)可以匹配换行符.如果没有这个参数,大多数情况下,用上面列正则表达式来消除网页HTML标记是无效的. 

HTML发展至今,语法已经相当复杂,上面只列出了几种最主要的标记,更多的去HTML标记的正则我将在 

Rost WebSpider 的开发过程中补充进来。 

下面用c#实现了一个从HTML字符串中提取有效内容的类: 

using System; 

using System.Collections.Generic; 

using System.Text; 

using System.Text.RegularExpressions; 

class HtmlExtract 

{ 

#region private attributes 

private string _strHtml; 

#endregion 

#region public mehtods 

public HtmlExtract(string inStrHtml) 

{ 

_strHtml = inStrHtml 

} 

public override string ExtractText() 

{ 

string result = _strHtml; 

result = RemoveComment(result); 

result = RemoveScript(result); 

result = RemoveStyle(result); 

result = RemoveTags(result); 

return result.Trim(); 

} 

#endregion 

#region private methods 

private string RemoveComment(string input) 

{ 

string result = input; 

//remove comment 

result = Regex.Replace(result, @"<!--[^-]*-->", string.Empty, RegexOptions.IgnoreCase); 

return result; 

} 

private string RemoveStyle(string input) 

{ 

string result = input; 

//remove all styles 

result = Regex.Replace(result, @"<style[^>]*?>.*?</style>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline); 

return result; 

} 

private string RemoveScript(string input) 

{ 

string result = input; 

result = Regex.Replace(result, @"<script[^>]*?>.*?</script>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline); 

result = Regex.Replace(result, @"<noscript[^>]*?>.*?</noscript>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline); 

return result; 

} 

private string RemoveTags(string input) 

{ 

string result = input; 

result = result.Replace(" ", " "); 

result = result.Replace(""", "\""); 

result = result.Replace("<", "<"); 

result = result.Replace(">", ">"); 

result = result.Replace("&", "&"); 

result = result.Replace("<br>", "\r\n"); 

result = Regex.Replace(result, @"<[\s\S]*?>", string.Empty, RegexOptions.IgnoreCase); 

return result; 

} 

#endregion

c# 正则表达式对网页进行内容抓取的更多相关文章

  1. WordPress快速增加百度收录,加快网站内容抓取

    本文已同步到专业技术网站 www.sufaith.com, 该网站专注于前后端开发技术与经验分享, 包含Web开发.Nodejs.Python.Linux.IT资讯等板块. 利用百度站长平台提供的链接 ...

  2. Python 网页投票信息抓取

    最近学习python,为了巩固一下学过的知识,花了半天(主要还是因为自己正则表达式不熟)写了个小脚本来抓取一个网站上的投票信息,排名后进行输出. 抓取的网站网址是http://www.mudidi.n ...

  3. 有了 Docker,用 JavaScript 框架开发的 Web 站点也能很好地支持网络爬虫的内容抓取

    点这里 阅读目录 用 AngularJS(以及其它 JavaScript 框架)开发的 Web 站点不支持爬虫的抓取 解决方案 为什么公开我们的解决方案 实现 AngularJS 服务 结论   Pr ...

  4. 爬虫小例1:ajax形式的网页数据的抓取

    ---恢复内容开始--- 下面记录如何抓取ajax形式加载的网页数据: 目标:获取“https://movie.douban.com/typerank?type_name=%E5%89%A7%E6%8 ...

  5. 网页爬虫--python3.6+selenium+BeautifulSoup实现动态网页的数据抓取,适用于对抓取频率不高的情况

    说在前面: 本文主要介绍如何抓取 页面加载后需要通过JS加载的数据和图片 本文是通过python中的selenium(pyhton包) + chrome(谷歌浏览器) + chromedrive(谷歌 ...

  6. 关于抓取js加载出来的内容抓取

    一.抓取页面 url=https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.ht ...

  7. 手机连接fiddler后,浏览器无法打开网页或者fiddler抓取不到手机应用相关数据的情况

    关于手机如何连接fiddler,网上有很多教程,我暂时就不写了 今天在使用fiddler的过程中,发现fiddler突然无法抓取移动端应用的数据包,再三确认连接无误.因此就开始了解决之旅 起因是安卓手 ...

  8. 一起来学习XPATH,来看看除了正则表达式我们还能怎么抓取数据

    参考学习的网站链接http://www.w3school.com.cn/xpath/xpath_intro.asp 首先理清楚一些常识 以此为例 <?xml version="1.0& ...

  9. Python爬虫之三种网页抓取方法性能比较

    下面我们将介绍三种抓取网页数据的方法,首先是正则表达式,然后是流行的 BeautifulSoup 模块,最后是强大的 lxml 模块. 1. 正则表达式   如果你对正则表达式还不熟悉,或是需要一些提 ...

随机推荐

  1. Android应用开发中Intent的作用及使用方法

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

  2. Android Service 通过 BroadcastReceiver 更新Activity UI

    1:MainActivity.java public class MainActivity extends Activity { private TextView tvInfo = null; pri ...

  3. EXW_FOB_CIF_CFR 外贸报价方式&条款之间的区别与联系

    一.工厂交货价(EXW=Ex Works): 交货地点:出口国工厂或仓库:  运 输:买方负责:  保 险:买方负责:  出口手续:买方负责:  进口手续:买方负责:  风险转移:交货地:  所有权转 ...

  4. SQL Server 索引的图形界面操作 <第十二篇>

    一.索引的图形界面操作 SQL Server非常强大的就是图形界面操作.关于索引方面也一样那么强大,很多操作比如说重建索引啊,查看各种统计信息啊,都能够通过图形界面快速查看和操作,下面来看看SQL S ...

  5. 一次使用Eclipse Memory Analyzer分析Tomcat内存溢出

    转:http://tivan.iteye.com/blog/1487855 前言 在平时开发.测试过程中.甚至是生产环境中,有时会遇到OutOfMemoryError,Java堆溢出了,这表明程序有严 ...

  6. C语言中long类型,int类型

    long类型表示long int,一般简写为long,注意long不表示long double.虽然有时候会有sizeof(long)=sizeof(int),long int与int是不同的: 16 ...

  7. k-d Tree in TripAdvisor

    Today, TripAdvisor held a tech talk in Columbia University. The topic is about k-d Tree implemented ...

  8. 静态链表实现 (A-B)U(B-A)

    图中黄色部分为(A-B)U(B-A)的实际意义,用结构数组做静态链表来实现该表达式 大致流程是先建立A链表,接着将挨个输入的B中元素在A链表中遍历.如果没找到,就加到A链表结尾下标为endpointe ...

  9. JOB+MERGE 跨服务器同步数据

    为了解决单服务器压力,将库分服务器部署,但是原来用触发器实现的表数据同步就实现不了了. 因为总监老大不允许 开启分布式事务(MSDTC),我又不想为了一个几千行的基础数据做复制订阅. 于是乎决定用 J ...

  10. poj 2411 Mondriaan's Dream(状态压缩dp)

    Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...