搜索引擎中一个比较重要的环节就是从网页中抽取出有效内容。简单来说,就是吧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# 正则表达式对网页进行有效内容抽取
2020-07-24 16:15正则之家 正则表达式
本文主要总结了用正则表达式对网页进行有效内容提取的具体实现方法,并给出了c#代码
延伸 · 阅读
- 2022-03-10C#使用RichTextBox实现替换文字及改变字体颜色功能
- 2022-03-10C#基于QRCode实现动态生成自定义二维码图片功能示
- 2022-03-10C#实现的上传图片、保存图片、加水印、生成缩略
- 2022-03-10Python全栈之正则表达式
- 2022-03-09C#使用base64对字符串进行编码和解码的测试
- 2022-03-09C# 10分钟完成百度人脸识别(入门篇)
- 正则表达式
解决preg_match匹配过多字符长度的限制的思路分析
今天在写采集程序的时候,使用到了preg_match,但是有几个页面始终采集不下来。反复看了N遍的正则,没有发现有问题。于是开始怀疑preg_match是否对匹配的...
- 正则表达式
JavaScript 正则表达式 验证整数、小数、实数、有效位小数最简单
JavaScript 正则表达式 验证整数、小数、实数、有效位小数最简单,输入完按回车后即可验证!(自认为最简单!)...
- 正则表达式
正则表达式概述 什么是正则表达式
正则表达式就是用某种模式去匹配一类字符串的公式,主要用来描述字符串匹配的工具...
- 正则表达式
js通过正则匹配没有内容的空标签
这篇文章主要介绍了js通过正则匹配没有内容的空标签,需要的朋友可以参考下...
- 正则表达式
asp去除html标记与空格的正则
用asp实现的去除内容的html标记和空格的实现代码,正在学习正则表达式的朋友可以参考下...
- 正则表达式
正则表达式re.sub替换不完整的问题及完整解决方案
re.sub是个正则表达式方面的函数,用来实现通过正则表达式,实现比普通字符串的replace更加强大的替换功能。这篇文章主要介绍了正则表达式re.sub替换不完...
- 正则表达式
常用正则表达式语法例句
本文主要分享一些常用正则表达式语法例句,具有一定的参考价值,有需要的朋友可以了解下...
- 正则表达式
JS正则表达式验证数字(非常全)
正则表达式,又称规则表达式,在项目中经常会用到正则表达式,今天小编抽空给大家分享js正则表达式验证数字的方法,感兴趣的朋友参考下吧...