c# 正则表达式对网页进行内容抓取
来源: 阅读:5361 次 日期:2014-07-30 13:59:50
温馨提示: 小编为您整理了“c# 正则表达式对网页进行内容抓取”,方便广大网友查阅!

搜索引擎中一个比较重要的环节就是从网页中抽取出有效内容。简单来说,就是吧HTML文本中的HTML标记去掉,留下我们用IE等浏览器打开HTML文档看到的部分(我们这里不考虑图片).

将HTML文本中的标记分为:注释,script ,style,以及其他标记分别去掉:

1.去注释,正则为:

output = Regex.Replace(input, @"", string.Empty, RegexOptions.IgnoreCase);

2.去script,正则为:

ouput = Regex.Replace(input, @"]*?>.*?", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);

output2 = Regex.Replace(ouput , @"]*?>.*?", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);

3.去style,正则为:

output = Regex.Replace(input, @"]*?>.*?", 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("
", "\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, @"]*?>.*?", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);

return result;

}

private string RemoveScript(string input)

{

string result = input;

result = Regex.Replace(result, @"]*?>.*?", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);

result = Regex.Replace(result, @"]*?>.*?", 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("
", "\r\n");

result = Regex.Replace(result, @"<[\s\S]*?>", string.Empty, RegexOptions.IgnoreCase);

return result;

}

#endregion

更多信息请查看IT技术专栏

更多信息请查看 网络编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2026上岸·考公考编培训报班

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 新媒体/短视频平台| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65099533/13759567129 获取招聘考试信息及咨询关注公众号:
咨询QQ:1093837350(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map