麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Regex > 正文

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

2020-03-16 21:17:15
字體:
來源:轉載
供稿:網友
本問主要總結了用正則表達式對網頁進行有效內容提取的具體實現方法,并給出了c#代碼
 
 
搜索引擎中一個比較重要的環節就是從網頁中抽取出有效內容。簡單來說,就是吧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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲午夜免费 | 亚洲一区二区中文字幕在线观看 | 久久草在线看 | 成人午夜在线播放 | 久久久日韩av免费观看下载 | 久久经典国产视频 | 少妇一级淫片高潮流水电影 | 欧美成人黄色小视频 | 欧美亚洲一区二区三区四区 | 欧美成人二区 | 中国洗澡偷拍在线播放 | 亚洲国产精品久久久久久久久久 | 精品一二三区视频 | 沉沦的校花奴性郑依婷c到失禁 | 香蕉秀| 国产午夜精品久久久久 | 黄污免费网站 | 56av国产精品久久久久久久 | 国产精品av久久久久久网址 | 欧美一级毛片欧美一级成人毛片 | 精品国产呦系列在线看 | 欧美一级黄视频 | 黄色网址免费在线播放 | 91精品国产日韩91久久久久久360 | 午夜视频在线 | 视频一区二区国产 | 国产羞羞视频在线观看免费应用 | 国产成年免费视频 | 欧美一级黄带 | 久久成人激情视频 | 久久久久久麻豆 | 日本a∨精品中文字幕在线 被啪羞羞视频在线观看 | av电影免费观看 | 久久免费观看一级毛片 | 国产在线免费 | 国产精品久久久久久久四虎电影 | 欧美a视频| 国产美女视频一区 | www成人在线观看 | 国内精品久久久久久久影视红豆 | 久久伊 |