在网页刚流行起来的时候,提取html中的文本有一个简单的方法,就是将html文本(包含标记)中的所有以“<”符号开头到以“>”符号之间的内容去掉即可。
但对于现在复杂的网页而言,用这种方法提取出来的文本会有大量的空格、空行、script段落、还有一些html转义字符,效果很差。
下面用正则表达式来提取html中的文本,
代码的实现的思路是:
a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)
b、将<head>标记中的所有内容去掉
c、将<script>标记中的所有内容去掉
d、将<style>标记中的所有内容去掉
e、将td换成空格,tr,li,br,p 等标记换成换行符
f、去掉所有以“<>”符号为头尾的标记去掉。
g、转换&,&nbps等转义字符换成相应的符号
h、去掉多余的空格和空行
代码如下:
using Systemusing System.Text.RegularExpressions
namespace Kwanhong.Utilities
{
/// <summary>
/// HtmlToText 的摘要说明。
/// </summary>
public class HtmlToText
{
public string Convert(string source)
{
string result
//remove line breaks,tabs
result = source.Replace("\r", " ")
result = result.Replace("\n", " ")
result = result.Replace("\t", " ")
//remove the header
result = Regex.Replace(result, "(<head>).*(</head>)", string.Empty, RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<( )*script([^>])*>", "<script>", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, RegexOptions.IgnoreCase)
//remove all styles
result = Regex.Replace(result, @"<( )*style([^>])*>", "<style>", RegexOptions.IgnoreCase) //clearing attributes
result = Regex.Replace(result, "(<style>).*(</style>)", string.Empty, RegexOptions.IgnoreCase)
//insert tabs in spaces of <td> tags
result = Regex.Replace(result, @"<( )*td([^>])*>", " ", RegexOptions.IgnoreCase)
//insert line breaks in places of <br> and <li> tags
result = Regex.Replace(result, @"<( )*br( )*>", "\r", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<( )*li( )*>", "\r", RegexOptions.IgnoreCase)
//insert line paragraphs in places of <tr> and <p> tags
result = Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", RegexOptions.IgnoreCase)
//remove anything thats enclosed inside < >
result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase)
//replace special characters:
result = Regex.Replace(result, @"&", "&", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @" ", " ", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"<", "<", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @">", ">", RegexOptions.IgnoreCase)
result = Regex.Replace(result, @"&(.{2,6})", string.Empty, RegexOptions.IgnoreCase)
//remove extra line breaks and tabs
result = Regex.Replace(result, @" ( )+", " ")
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r")
result = Regex.Replace(result, @"(\r\r)+", "\r\n")
return result
}
}//end class
}//end namespace
/// <summary>/// 去除HTML标记
/// </summary>
/// <param name="NoHTML">包括HTML的源码 </param>
/// <returns>已经去除后的文字</returns>
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring,@"<script[^>]*?>.*?</script>","",RegexOptions.IgnoreCase)
//删除HTML
Htmlstring = Regex.Replace(Htmlstring,@"<(.[^>]*)>","",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"([\r\n])[\s]+","",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"-->","",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"<!--.*","",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(quot|#34)","\"",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(amp|#38)","&",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(lt|#60)","<",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(gt|#62)",">",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(nbsp|#160)"," ",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(iexcl|#161)","\xa1",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(cent|#162)","\xa2",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(pound|#163)","\xa3",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring,@"&(copy|#169)","\xa9",RegexOptions.IgnoreCase)
Htmlstring = Regex.Replace(Htmlstring, @"(\d+)","",RegexOptions.IgnoreCase)
Htmlstring.Replace("<","")
Htmlstring.Replace(">","")
Htmlstring.Replace("\r\n","")
//Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim()
return Htmlstring
}
去除字符串中的html标签: 1 public static string Html2Text(string htmlStr)2
3 {
4
5 if (String.IsNullOrEmpty(htmlStr))
6
7 {
8
9 return ""
10
11 }
12
13 string regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>" //定义style的正则表达式
14
15 string regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>" //定义script的正则表达式
16
17 string regEx_html = "<[^>]+>" //定义HTML标签的正则表达式
18
19 htmlStr = Regex.Replace(htmlStr, regEx_style, "")//删除css
20
21 htmlStr = Regex.Replace(htmlStr, regEx_script, "")//删除js
22
23 htmlStr = Regex.Replace(htmlStr, regEx_html, "")//删除html标记
24
25 htmlStr = Regex.Replace(htmlStr, "\\s*|\t|\r|\n", "")//去除tab、空格、空行
26
27 htmlStr = htmlStr.Replace(" ", "")
28
29 htmlStr = htmlStr.Replace(""", "")//去除异常的引号" " "
30
31 htmlStr = htmlStr.Replace(""", "")
32
33 return htmlStr.Trim()
34
35 }