中文字幕免费精品_亚洲视频自拍_亚洲综合国产激情另类一区_色综合咪咪久久

c# 正則表達式對網頁進行內容抓取
來源:易賢網 閱讀:5169 次 日期: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, @"<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

更多信息請查看IT技術專欄

更多信息請查看網絡編程
易賢網手機網站地址:c# 正則表達式對網頁進行內容抓取
由于各方面情況的不斷調整與變化,易賢網提供的所有考試信息和咨詢回復僅供參考,敬請考生以權威部門公布的正式信息和咨詢為準!

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
云南網警備案專用圖標
聯系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網
云南網警報警專用圖標
主站蜘蛛池模板: 资阳市| 雷山县| 微博| 凤冈县| 五台县| 定州市| 临澧县| 申扎县| 鹰潭市| 襄垣县| 永春县| 龙泉市| 江陵县| 富锦市| 蛟河市| 西和县| 青浦区| 鄂托克旗| 抚州市| 定襄县| 富蕴县| 乐安县| 丹阳市| 嘉黎县| 沂水县| 丰县| 凤冈县| 阜新| 通河县| 沁水县| 平和县| 河西区| 石屏县| 泸西县| 陇南市| 武川县| 莒南县| 贵港市| 镶黄旗| 惠州市| 裕民县|