国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Regex > 正文

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

2020-03-16 21:17:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
本問(wèn)主要總結(jié)了用正則表達(dá)式對(duì)網(wǎng)頁(yè)進(jìn)行有效內(nèi)容提取的具體實(shí)現(xiàn)方法,并給出了c#代碼
 
 
搜索引擎中一個(gè)比較重要的環(huán)節(jié)就是從網(wǎng)頁(yè)中抽取出有效內(nèi)容。簡(jiǎn)單來(lái)說(shuō),就是吧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ù),這個(gè)參數(shù)很重要,他主要是為了讓"."(小圓點(diǎn))可以匹配換行符.如果沒有這個(gè)參數(shù),大多數(shù)情況下,用上面列正則表達(dá)式來(lái)消除網(wǎng)頁(yè)HTML標(biāo)記是無(wú)效的. 
HTML發(fā)展至今,語(yǔ)法已經(jīng)相當(dāng)復(fù)雜,上面只列出了幾種最主要的標(biāo)記,更多的去HTML標(biāo)記的正則我將在 
Rost WebSpider 的開發(fā)過(guò)程中補(bǔ)充進(jìn)來(lái)。 
下面用c#實(shí)現(xiàn)了一個(gè)從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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 嵊泗县| 武宣县| 凌云县| 瓮安县| 北碚区| 江源县| 卢龙县| 山东省| 来安县| 南康市| 汉中市| 中宁县| 沅陵县| 广宁县| 新宾| 岑巩县| 准格尔旗| 策勒县| 额济纳旗| 新闻| 徐汇区| 岳阳县| 神农架林区| 鞍山市| 肥西县| 原阳县| 宜阳县| 涞水县| 内乡县| 彩票| 灌云县| 宁强县| 石景山区| 伽师县| 抚州市| 金川县| 华亭县| 津市市| 洞头县| 和硕县| 铅山县|