婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > c# 正則表達式對網頁進行有效內容抽取

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

熱門標簽:RO地圖標注app 高德地圖標注短信簽約 自制電銷機器人 湖南企業智能外呼系統供應商 福州工作銷售電話機器人 電銷機器人公司簡介 百音電話機器人 知名的電話機器人 錫林郭勒盟地圖標注位置
搜索引擎中一個比較重要的環節就是從網頁中抽取出有效內容。簡單來說,就是吧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("nbsp;", " ");
result = result.Replace("quot;", "\"");
result = result.Replace("lt;", "");
result = result.Replace("gt;", ">");
result = result.Replace("amp", "");
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("nbsp;", " ");
result = result.Replace("quot;", "\"");
result = result.Replace("lt;", "");
result = result.Replace("gt;", ">");
result = result.Replace("amp", "");
result = result.Replace("br>", "\r\n");
result = Regex.Replace(result, @"[\s\S]*?>", string.Empty, RegexOptions.IgnoreCase);
return result;
}
#endregion
您可能感興趣的文章:
  • 使用C# Winform應用程序獲取網頁源文件的解決方法
  • C#基于正則表達式實現獲取網頁中所有信息的網頁抓取類實例
  • 使用C#正則表達式獲取必應每日圖片地址
  • C#正則表達式獲取下拉菜單(select)的相關屬性值
  • C#使用正則表達式抓取網站信息示例
  • C#通過正則表達式實現提取網頁中的圖片
  • 常用正則 常用的C#正則表達式
  • C#的正則表達式Regex類使用簡明教程
  • C# 正則表達式經典分類整理集合手冊
  • C#中的正則表達式 學習資料
  • WinForm使用正則表達式提取內容的方法示例

標簽:秦皇島 茂名 怒江 西寧 玉林 昆明 河北 吉林

巨人網絡通訊聲明:本文標題《c# 正則表達式對網頁進行有效內容抽取》,本文關鍵詞  正則,表達式,對,網頁,進行,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《c# 正則表達式對網頁進行有效內容抽取》相關的同類信息!
  • 本頁收集關于c# 正則表達式對網頁進行有效內容抽取的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 黎平县| 华亭县| 阳朔县| 阜南县| 宝坻区| 泰州市| 乌兰县| 南投市| 波密县| 泸水县| 岑巩县| 化州市| 寻乌县| 化隆| 获嘉县| 苏尼特右旗| 濮阳市| 谢通门县| 福泉市| 富川| 屏边| 玛纳斯县| 合江县| 英吉沙县| 清水县| 子洲县| 石楼县| 玛曲县| 云南省| 邵阳市| 武夷山市| 阜城县| 清流县| 泸西县| 祁连县| 威海市| 平谷区| 清水河县| 永登县| 雅江县| 辽阳市|