a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > regex > 正文

c# 正則表達式對網頁進行有效內容抽取

2020-01-20 22:22:34
字體:
來源:轉載
供稿:網友
搜索引擎中一個比較重要的環節就是從網頁中抽取出有效內容。簡單來說,就是吧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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 极情综合网 | 色草在线 | 天天曰夜夜操 | 黄色毛片视频在线观看 | 精品视频二区三区 | 天堂伊人网 | 国产乱码精品一区二区三区忘忧草 | 日本中文字幕在线观看 | 久久久久久综合 | 91在线精品秘密一区二区 | 在线播放国产一区二区三区 | 久久久久.com| 久久99精品国产.久久久久 | 欧美激情在线狂野欧美精品 | 忘忧草精品久久久久久久高清 | 91精品国产综合久久久久久蜜臀 | 青青久视频 | 欧美激情一区二区三区蜜桃视频 | 亚洲欧美国产精品久久久久 | 精品99在线 | 亚洲在线视频 | 日韩精品免费 | www.久久.com | 中文字幕av第一页 | 妞干网在线观看 | 欧美一区二区三区在线观看 | 久久精品免费观看 | 国产久| 亚洲福利在线播放 | 亚洲一区二区伦理 | 亚洲在线免费观看 | 成人av国产 | 日韩电影在线看 | 黄色精品视频 | 成人免费crm在线观看 | 怡红院免费在线视频 | 蜜桃色网| 米奇成人网 | 日韩一区二区三区av | 91视频免费版污 | 在线观看日韩av |