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

2016-02-19 10:05 63 1 收藏

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐c# 正则表达式对网页进行有效内容抽取,赶紧看过来吧!

【 tulaoshi.com - Web开发 】

搜索引擎中一个比较重要的环节就是从网页中抽取出有效内容。简单来说,就是吧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", "rn");
result = Regex.Replace(result, @"[sS]*?", 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", "rn");
result = Regex.Replace(result, @"[sS]*?", string.Empty, RegexOptions.IgnoreCase);
return result;
}
#endregion

来源:https://www.tulaoshi.com/n/20160219/1593325.html

延伸阅读
标签: Web开发
正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为元字符)。模式描述在搜索文本时要匹配的一个或多个字符串。   正则表达式示例 表达式 匹配 /^\s*$/ 匹配空行。 /\d{2}-\d{5}/ 验证由两位数字、一个连字符再加 5 位数字组成的 ID 号。 /\s*(\S+)(\s[^]*)?[\s\S]*\s*\/\1\s*/ ...
标签: Web开发
正则表达式在PHP中被用来处理复杂的文字串。支持正则表达式的函数有: ereg()ereg replace()eregi replace()split() 这些函数都将正则表达式作为他们的第一个参数。PHP使用POSIX扩展规则表达式(使用POSIX 1003.2)。要找到所有的关于POSIX扩展规则表达式的描述,请查看包括在PHP发行版本之内的regex man页面。 Examp...
正则表达式简介 翻译:NorthTibet 原文出处:Regular Expressions 有些新手对正则表达式不是很熟悉,有必要在此作一简单回顾。如果你是正则表达式高手,可以不用看这一部分。 正则表达式是描述字符串集的字符串。例如,正则表达式“Mic*”描述所有包含“Mic”,后跟零个或多个字符的字符串。Mickey、Microsoft、Michelangelo...
标签: Web开发
####################### #作者:雨浪 版权所有,翻版说一下 # #QQ:270499458 # ####################### 近段日子几个刚学了正则表达式的朋友问我在asp中怎么用.呵呵.虽然简单,还是写出来吧,正则表达式的基本知识我就不说了.其实已经有...
标签: Web开发
前言 正则表达式是烦琐的,但是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感。只要认真去阅读这些资料,加上应用的时候进行一定的参考,掌握正则表达式不是问题。 索引 1. 引子 目前,正则表达式已经在很多软件中得到广泛的应用,包括*nix(Linux, Unix等),HP等操作系统,PHP,C#,Java等开发环境,以...

经验教程

128

收藏

15
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部