主頁 > 知識(shí)庫 > c# 正則表達(dá)式對(duì)網(wǎng)頁進(jìn)行有效內(nèi)容抽取

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

熱門標(biāo)簽:RO地圖標(biāo)注app 高德地圖標(biāo)注短信簽約 自制電銷機(jī)器人 湖南企業(yè)智能外呼系統(tǒng)供應(yīng)商 福州工作銷售電話機(jī)器人 電銷機(jī)器人公司簡介 百音電話機(jī)器人 知名的電話機(jī)器人 錫林郭勒盟地圖標(biāo)注位置
搜索引擎中一個(gè)比較重要的環(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("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參數(shù),這個(gè)參數(shù)很重要,他主要是為了讓"."(小圓點(diǎn))可以匹配換行符.如果沒有這個(gè)參數(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#實(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("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應(yīng)用程序獲取網(wǎng)頁源文件的解決方法
  • C#基于正則表達(dá)式實(shí)現(xiàn)獲取網(wǎng)頁中所有信息的網(wǎng)頁抓取類實(shí)例
  • 使用C#正則表達(dá)式獲取必應(yīng)每日?qǐng)D片地址
  • C#正則表達(dá)式獲取下拉菜單(select)的相關(guān)屬性值
  • C#使用正則表達(dá)式抓取網(wǎng)站信息示例
  • C#通過正則表達(dá)式實(shí)現(xiàn)提取網(wǎng)頁中的圖片
  • 常用正則 常用的C#正則表達(dá)式
  • C#的正則表達(dá)式Regex類使用簡明教程
  • C# 正則表達(dá)式經(jīng)典分類整理集合手冊(cè)
  • C#中的正則表達(dá)式 學(xué)習(xí)資料
  • WinForm使用正則表達(dá)式提取內(nèi)容的方法示例

標(biāo)簽:秦皇島 茂名 怒江 西寧 玉林 昆明 河北 吉林

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《c# 正則表達(dá)式對(duì)網(wǎng)頁進(jìn)行有效內(nèi)容抽取》,本文關(guān)鍵詞  正則,表達(dá)式,對(duì),網(wǎng)頁,進(jìn)行,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《c# 正則表達(dá)式對(duì)網(wǎng)頁進(jìn)行有效內(nèi)容抽取》相關(guān)的同類信息!
  • 本頁收集關(guān)于c# 正則表達(dá)式對(duì)網(wǎng)頁進(jìn)行有效內(nèi)容抽取的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章