1 using System;
  2 using System.Text;
  3 namespace HtmlStrip
  4 {
  5     class MainClass
  6     {
  7         public static void Main (string[] args)
  8         {
  9             string str = "<div>abc</div><span>efg</span><br /><script>888</script><!--<PA>WW</PA-->oo";
 10             //System.IO.StreamReader rd=new System.IO.StreamReader ("/home/lx/test.html");
 11             //str=rd.ReadToEnd ();
 12             HtmlParser t = new HtmlParser (str); //
 13             t.KeepTag (new string[] { "br" }); //设置br标签不过虑
 14             Console.Write (t.Text ());
 15         }
 16         
 17         
 18         
 19     }
 20     class HtmlParser
 21     {
 22         private string[] htmlcode; //把html转为数组形式用于分析
 23         private StringBuilder result = new StringBuilder ();  //输出的结果
 24         private int seek; //分析文本时候的指针位置
 25         private string[] keepTag;  //用于保存要保留的尖括号内容
 26         private bool _inTag;  //标记现在的指针是不是在尖括号内
 27         private bool needContent = true;  //是否要提取正文
 28         private string tagName;  //当前尖括号的名字
 29         private string[] specialTag = new string[] { "script", "style", "!--" };  //特殊的尖括号内容,一般这些标签的正文是不要的
 30         
 31         /// <summary>
 32         /// 当指针进入尖括号内,就会触发这个属性。这里主要逻辑是提取尖括号里的标签名字
 33         /// </summary>
 34         public bool inTag {
 35             get { return _inTag; }
 36             set {
 37                 _inTag = value;
 38                 if (!value)
 39                     return;
 40                 bool ok = true;
 41                 tagName = "";
 42                 while (ok) {
 43                     string word = read ();
 44                     if (word != " " && word != ">") {
 45                         tagName += word;
 46                     } else if (word == " " && tagName.Length > 0) {
 47                         ok = false;
 48                     } else if (word == ">") {
 49                         ok = false;
 50                         inTag = false;
 51                         seek -= 1;
 52                     }
 53                 }
 54             }
 55         }
 56         /// <summary>
 57         /// 初始化类
 58         /// </summary>
 59         /// <param name="html">
 60         ///  要分析的html代码
 61         /// </param>
 62         public HtmlParser (string html)
 63         {
 64             htmlcode = new string[html.Length];
 65             for (int i = 0; i < html.Length; i++) {
 66                 htmlcode[i] = html[i].ToString ();
 67             }
 68             KeepTag (new string[] {  });
 69         }
 70         /// <summary>
 71         /// 设置要保存那些标签不要被过滤掉
 72         /// </summary>
 73         /// <param name="tags">
 74         ///
 75         /// </param>
 76         public void KeepTag (string[] tags)
 77         {
 78             keepTag = tags;
 79         }
 80         
 81         /// <summary>
 82         /// 
 83         /// </summary>
 84         /// <returns>
 85         /// 输出处理后的文本
 86         /// </returns>
 87         public string Text ()
 88         {
 89             int startTag = 0;
 90             int endTag = 0;
 91             while (seek < htmlcode.Length) {
 92                 string word = read ();
 93                 if (word.ToLower () == "<") {
 94                     startTag = seek;
 95                     inTag = true;
 96                 } else if (word.ToLower () == ">") {
 97                     endTag = seek;
 98                     inTag = false;
 99                     if (iskeepTag (tagName.Replace ("/", ""))) {
100                         for (int i = startTag - 1; i < endTag; i++) {
101                             result.Append (htmlcode[i].ToString ());
102                         }http://www.huiyi8.com/clxgt/​
103                     } else if (tagName.StartsWith ("!--")) {
104                         bool ok = true;窗帘效果图
105                         while (ok) {
106                             if (read () == "-") {
107                                 if (read () == "-") {
108                                     if (read () == ">") {
109                                         ok = false;
110                                     } else {
111                                         seek -= 1;
112                                     }
113                                 }
114                             }
115                         }
116                     } else {
117                         foreach (string str in specialTag) {
118                             if (tagName == str) {
119                                 needContent = false;
120                                 break;
121                             } else
122                                 needContent = true;
123                         }
124                     }
125                 } else if (!inTag && needContent) {
126                     result.Append (word);
127                 }
128                 
129             }
130             return result.ToString ();
131         }
132         /// <summary>
133         /// 判断是否要保存这个标签
134         /// </summary>
135         /// <param name="tag">
136         /// A <see cref="System.String"/>
137         /// </param>
138         /// <returns>
139         /// A <see cref="System.Boolean"/>
140         /// </returns>
141         private bool iskeepTag (string tag)
142         {
143             foreach (string ta in keepTag) {
144                 if (tag.ToLower () == ta.ToLower ()) {
145                     return true;
146                 }
147             }
148             return false;
149         }
150         private string read ()
151         {
152             return htmlcode[seek++];
153         }
154 
155     }
156 }
157

提取html的正文的更多相关文章

  1. c# 使用正则表达式 提取章节小说正文全本篇

    这一节主要内容是使用正则表达式提取网站的正文,主要面向于小说章节网站.其中涉及到一些其他知识点,比如异步读取.异步流写入等,代码中都会有详细的注解.现在流行的网络文学都是每日一更或几更,没有一个统一的 ...

  2. 提取HTML的正文类

    本文转载:http://blog.csdn.net/cjh200102/article/details/6824895 //2.提取html的正文 类 using System; using Syst ...

  3. 利用正则提取discuz的正文内容

    源正文: [p=24, null, left][color=#000][font=宋体]近日,香港著名漫画家马荣成在香港举办的"[color=#ff660][url=http://cul.c ...

  4. HTML 转文本及HTML内容提取(C#)

    //1.HTML直接转文本 //使用方法 HtmlToText convert = new HtmlToText(); textBox2.Text = convert.Convert(textBox1 ...

  5. Java 提取Word中的文本和图片

    本文将介绍通过Java来提取或读取Word文档中文本和图片的方法.这里提取文本和图片包括同时提取文档正文当中以及页眉.页脚中的的文本和图片. 使用工具:Free Spire.Doc for Java ...

  6. Python网页正文转换语音文件的操作方法

    天气真的是越来越冷啦,有时候我们想翻看网页新闻,但是又冷的不想把手拿出来,移动鼠标翻看.这时候,是不是特别想电脑像讲故事一样,给我们念出来呢?人生苦短,我有python啊,试试用 Python 来朗读 ...

  7. UWP开发入门(二十三)——WebView

    本篇讨论在UWP开发中使用WebView控件时常见的问题,以及一些小技巧. WebView是实际开发中常用的控件,很多大家抱怨的套网页的应用都是通过WebView来实现的.这里要澄清一个问题,套网页的 ...

  8. JAVA爬虫 WebCollector

    JAVA爬虫 WebCollector 爬虫简介: WebCollector是一个无须配置.便于二次开发的JAVA爬虫框架(内核),它提供精简的的API,只需少量代码即可实现一个功能强大的爬虫. 爬虫 ...

  9. Python爬虫初学(二)—— 爬百度贴吧

    Python爬虫初学(二)-- 爬百度贴吧 昨天初步接触了爬虫,实现了爬取网络段子并逐条阅读等功能,详见Python爬虫初学(一). 今天准备对百度贴吧下手了,嘿嘿.依然是跟着这个博客学习的,这次仿照 ...

随机推荐

  1. dprobe and dprofile

    https://github.com/xwlan dprofiler Lightweight CPU Memory I/O and Lock profiler for Windows dprobe D ...

  2. jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径

    遇到的问题是这种,在jsp网页中嵌入了本地的图片,由于会用到上传到服务器的图片,所以没有放到项目里面,而是把全部图片单独放到一个文件夹里,然后打算使用绝对路径把要显示的图片显示出来.比方是放在了E盘的 ...

  3. Flash如何为文字描边

    可以使用墨水瓶工具,但是要先把文字打散(可以打散之后再组合起来)粗细和颜色都可以调,粗细就是笔触,颜色就是前景色(边框颜色)  

  4. 最全的HTTP头部信息分析

    HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...

  5. 用C++设计一个不能被继承的类(转)

    在Java 中定义了关键字final,被final修饰的类不能被继承. 首先想到的是在C++中,子类的构造函数会自动调用父类的构造函数.同样,子类的析构函数也会自动调用父类的析构函数.要想一个类不能被 ...

  6. UIView 的 autoresizingMask 属性 详解。

    转载自:liubo0_0的专栏  链接网址:http://blog.csdn.net/liubo0_0/article/details/7085935 在 UIView 中有一个autoresizin ...

  7. javascript 高级编程系列 - 继承

    1. 原型链继承 (缺点:子类继承父类的引用类型的属性值会在各个实例中共享,创建子类实例时无法向父类构造函数传递参数) // 定义父类构造函数 function SuperClass(father, ...

  8. 手把手教你nginx/linux下如何增加网站

    先进入到nginx的配置文件目录请输入以下命令 cd /alidata/server/nginx/conf/vhosts 再输入   ll 看看是不是像下面截图的一样 用神器xftp将default. ...

  9. Python--常用模块部分

    模块 pip install #模块名称 #安装模块 #导入模块 from collections import namedtuple collections模块 提供了几个额外的数据类型: Coun ...

  10. websotrom 2016.2 license Server

    license server” 输入:http://114.215.133.70:41017 仅供学习测试使用,支持正版.