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

首頁 > 編程 > regex > 正文

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

2020-01-20 22:22:34
字體:
來源:轉載
供稿:網友
搜索引擎中一個比較重要的環節就是從網頁中抽取出有效內容。簡單來說,就是吧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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 胶州市| 永顺县| 肃南| 海门市| 获嘉县| 德安县| 深圳市| 广汉市| 荔浦县| 通化县| 沁源县| 南昌市| 仙桃市| 阆中市| 汽车| 宽城| 九寨沟县| 资阳市| 乐至县| 浙江省| 清河县| 琼海市| 土默特右旗| 阿克| 新密市| 鄂托克前旗| 宕昌县| 聊城市| 丘北县| 抚顺市| 芦山县| 澄城县| 安多县| 冀州市| 贵德县| 五河县| 商洛市| 称多县| 视频| 成安县| 慈利县|