原文 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. mysqlbackup

    mysqlbackup 使用学习 1.设置数据库用户的相关权限 '; grant reload,replication client,super,process on *.* to backupuse ...

  2. Effective Java单元测试TestNG - 就是爱Java

    TestNG是另一种单元测试的framework,与JUnit的类似,这次Mix将使用它来撰写测试程序,大部分所引用的class package都一样,只差在JUnit与TestNG的字样,可以直接用 ...

  3. NFC应用(一)卡应用

    门禁卡.停车卡.公交卡工作于NFC的卡模式,是目前日常生活中接触得最多的NFC应用场合.一张小小的卡片,轻触读卡器使可开门禁锁.进出停车场.支付车资,即快捷方便,又安全,易于管理. 以门禁系统为例,通 ...

  4. 开源欣赏wordpress之intall.php

    引导式安装 $weblog_title = isset( $_POST['weblog_title'] ) ? trim( wp_unslash( $_POST['weblog_title'] ) ) ...

  5. HOWTO Use Python in the web — Python v3.0.1 documentation

    HOWTO Use Python in the web - Python v3.0.1 documentation mod_python¶ People coming from PHP often f ...

  6. HBase 5、Phoenix使用

    1.建表 执行建表语句 $ . ../examples/stock_symbol.sql 其中../examples/stock_symbol.sql是建表的sql语句 CREATE TABLE IF ...

  7. Android学习总结——文件储存

    Android中文件存储的操作: 1.Activity的openFileOutput()方法可以把数据输出到文件中2.创建的文件保存在/data/data/<package name>/f ...

  8. 【Java基础】可变参数

    下面是一个简单的小程序: import java.util.Arrays; class lesson6 { public static void main(String[] args) { ,,,,, ...

  9. Reverse Linked List II java

    public static ListNode reverseBetween(ListNode head, int m, int n) { ListNode pre=head,current=head, ...

  10. html基础之 input:type

    Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍.1,type=text输入类型是text,这是我们见的最多 ...