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

首頁 > 編程 > Regex > 正文

c# 正則表達(dá)式對網(wǎng)頁進(jìn)行有效內(nèi)容抽取

2020-03-16 21:17:15
字體:
供稿:網(wǎng)友
本問主要總結(jié)了用正則表達(dá)式對網(wǎng)頁進(jìn)行有效內(nèi)容提取的具體實現(xiàn)方法,并給出了c#代碼
 
 
搜索引擎中一個比較重要的環(huán)節(jié)就是從網(wǎng)頁中抽取出有效內(nèi)容。簡單來說,就是吧HTML文本中的HTML標(biāo)記去掉,留下我們用IE等瀏覽器打開HTML文檔看到的部分(我們這里不考慮圖片). 
將HTML文本中的標(biāo)記分為:注釋,script ,style,以及其他標(biāo)記分別去掉: 
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標(biāo)記 
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參數(shù),這個參數(shù)很重要,他主要是為了讓"."(小圓點)可以匹配換行符.如果沒有這個參數(shù),大多數(shù)情況下,用上面列正則表達(dá)式來消除網(wǎng)頁HTML標(biāo)記是無效的. 
HTML發(fā)展至今,語法已經(jīng)相當(dāng)復(fù)雜,上面只列出了幾種最主要的標(biāo)記,更多的去HTML標(biāo)記的正則我將在 
Rost WebSpider 的開發(fā)過程中補(bǔ)充進(jìn)來。 
下面用c#實現(xiàn)了一個從HTML字符串中提取有效內(nèi)容的類: 
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
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 九九热这里只有精品6 | 黄页视频在线免费观看 | 免费看色 | 黄色在线免费观看 | 久久久国产一区二区三区 | 韩国一区二区视频 | 日本精品一区二区三区视频 | 久久精品伊人 | 久久久久久亚洲 | 午夜在线视频免费观看 | 天堂√在线观看一区二区 | 欧美第7页| 久久久精品一区 | www.日韩大片 | 精品成人国产 | 99久久免费精品国产男女性高好 | 黄色免费看片 | 国产区最新 | 九九热精品免费视频 | 欧美色视| 国产精品久久精品 | 精品国产一区二区三区久久影院 | av成人在线观看 | av一二三区 | 日本精品在线视频 | 毛片毛片毛片毛片毛片毛片毛片毛片 | 欧美日韩免费看 | 91精品久久久久久久久久 | 日韩在线视频免费看 | 黄页网站免费观看 | 爱爱精品 | 一区二区在线影院 | 久久99精品国产麻豆不卡 | 欧美在线a | a级性视频| 免费在线一区二区三区 | 五月色综合 | 日韩一区二区在线视频 | 亚洲国产精品一区二区久久 | 不卡在线 | 韩国三级中文字幕hd久久精品 |