原文 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. Fragment销毁时replace和add两个方法的区别

    这个首先从一个bug说起,如图:   我们都知道fragment切换有两种方式: 1. replace方式 transaction.replace(R.id.content, IndexFragmen ...

  2. 嵌入式设备web服务器

    操作系统:ubuntu10.04 前言:    为了提高对设备的易操作性,很多设备中提供pc机直接通过浏览器操作设备的功能.这就需要在设备中实现web服务器.    现在在嵌入式设备中所使用的web服 ...

  3. 有序线性搜索(Sorted/Ordered Linear Search)

    如果数组元素已经排过序(升序),那我们搜索某个元素就不必遍历整个数组了.在下面给出的算法代码中,到任何一点,假设当前的arr[i]值大于搜索的值data,就可以停止搜索了. #include<s ...

  4. 【转】Android LCD(一):LCD基本原理篇

    关键词:android LCD TFT 液晶 偏光片 彩色滤光片  背光 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:samsung exyno ...

  5. ERROR: HHH000388: Unsuccessful: create table

    做SSH整合的时候,总是出现错误信息: 类似这样: : HHH000388: Unsuccessful: create table right (right_code varchar(255) not ...

  6. Unity 鼠标点击左右移动,人物跟随旋转

    上代码: using UnityEngine; using System.Collections; public class Test : MonoBehaviour { private Vector ...

  7. Han Move(细节题)

    Problem 1609 - Han Move Time Limit: 1000MS   Memory Limit: 65536KB    Total Submit: 620  Accepted: 1 ...

  8. C,C#,C++中&&和||,&和|的联系和区别

    本文来自:http://www.cnblogs.com/GT_Andy/archive/2010/03/30/1921805.html 两者计算结果相同(针对各自的运算对象),只是性能上有差别而已. ...

  9. Python 单词字母顺序不变且所有倒排

    翻出google測试project师的一道题目: 设计一个函数,不论什么语言都能够,实现下面功能: 一个句子,将句子中的单词所有倒排过来,但单词的字母顺序不变.eg.  this is a real ...

  10. Error:Execution failed for task &#39;:app:dexDebug&#39;. &gt; com.android.ide.common.process.ProcessException

    异常Log: Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessE ...