c# 正则表达式对网页进行有效内容抽取

所属分类: 网络编程 / 正则表达式 阅读数: 814
收藏 0 赞 0 分享
搜索引擎中一个比较重要的环节就是从网页中抽取出有效内容。简单来说,就是吧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("&nbsp;", " ");
result = result.Replace("&quot;", "\"");
result = result.Replace("&lt;", "<");
result = result.Replace("&gt;", ">");
result = result.Replace("&amp", "&");
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("&nbsp;", " ");
result = result.Replace("&quot;", "\"");
result = result.Replace("&lt;", "<");
result = result.Replace("&gt;", ">");
result = result.Replace("&amp", "&");
result = result.Replace("<br>", "\r\n");
result = Regex.Replace(result, @"<[\s\S]*?>", string.Empty, RegexOptions.IgnoreCase);
return result;
}
#endregion
更多精彩内容其他人还在看

正则表达式匹配 非XXX的行

问题:如何匹配"非:.+123.123.123.10.+ " 行
收藏 0 赞 0 分享

正则表达式不包含属性

一个标签里不包含某个属性 的 正则表达式的写法
收藏 0 赞 0 分享

ASP正则函数替换分页后的参数

在分页系统里面用到的把page后面得东西都给丢掉
收藏 0 赞 0 分享

asp match正则函数使用Matchs实例

asp matchs函数提供了对正则表达式匹配的只读属性的访问。一直都用这个函数,没想到本站竟然没有这类文章,汗一个,最近我会多加一些这样的文章
收藏 0 赞 0 分享

asp 图片正则 替换,替换前检查图片是不是本地地址的方法

这个图片正则先检查图片的地址,不是本地的则用本地的asp突破盗链,方便使用,注意是答chinaz的朋友问的一个问题
收藏 0 赞 0 分享

java正则表达式彻底研究

从J2SE1.4起Java增加了对正则表达式的支持就是java.util.regex包
收藏 0 赞 0 分享

正则表达式口诀 正则表达式学习工具

正则表达式口诀 + 常用的正则表达式 + 正则表达式学习工具+正则处理工具 正则是每个程序员绕不开的堡垒,只有把它攻下来。我觉得正则之所以难,第一难是需要记忆,第二难是要求具备抽象逻辑思维。
收藏 0 赞 0 分享

比较实用的正则表达式学习笔记

最近在学习正则,一些比较有用的东西怕忘记,记下来,比较乱,想一条记录一条
收藏 0 赞 0 分享

asp只采集网站可见文本的正则

它可以过虑Js 可以过滤 CSS 过滤HTML标识,只采集页面的可见文本。
收藏 0 赞 0 分享

asp.net常用正则表达式

比较常用的多种语言支持的正则整理收集
收藏 0 赞 0 分享
查看更多