本文转载:http://blog.csdn.net/cjh200102/article/details/6824895

//2、提取html的正文 类
using System;
using System.Text;
namespace HtmlStrip
{
class MainClass
{
public static void Main (string[] args)
{
string str = "<div>abc</div><span>efg</span><br /><script>888</script><!--<PA>WW</PA-->oo";
//System.IO.StreamReader rd=new System.IO.StreamReader ("/home/lx/test.html");
//str=rd.ReadToEnd ();
HtmlParser t = new HtmlParser (str); //
t.KeepTag (new string[] { "br" }); //设置br标签不过虑
Console.Write (t.Text ());
} }
class HtmlParser
{
private string[] htmlcode; //把html转为数组形式用于分析
private StringBuilder result = new StringBuilder (); //输出的结果
private int seek; //分析文本时候的指针位置
private string[] keepTag; //用于保存要保留的尖括号内容
private bool _inTag; //标记现在的指针是不是在尖括号内
private bool needContent = true; //是否要提取正文
private string tagName; //当前尖括号的名字
private string[] specialTag = new string[] { "script", "style", "!--" }; //特殊的尖括号内容,一般这些标签的正文是不要的 /// <summary>
/// 当指针进入尖括号内,就会触发这个属性。这里主要逻辑是提取尖括号里的标签名字
/// </summary>
public bool inTag {
get { return _inTag; }
set {
_inTag = value;
if (!value)
return;
bool ok = true;
tagName = "";
while (ok) {
string word = read ();
if (word != " " && word != ">") {
tagName += word;
} else if (word == " " && tagName.Length > 0) {
ok = false;
} else if (word == ">") {
ok = false;
inTag = false;
seek -= 1;
}
}
}
}
/// <summary>
/// 初始化类
/// </summary>
/// <param name="html">
/// 要分析的html代码
/// </param>
public HtmlParser (string html)
{
htmlcode = new string[html.Length];
for (int i = 0; i < html.Length; i++) {
htmlcode[i] = html[i].ToString ();
}
KeepTag (new string[] { });
}
/// <summary>
/// 设置要保存那些标签不要被过滤掉
/// </summary>
/// <param name="tags">
///
/// </param>
public void KeepTag (string[] tags)
{
keepTag = tags;
} /// <summary>
///
/// </summary>
/// <returns>
/// 输出处理后的文本
/// </returns>
public string Text ()
{
int startTag = 0;
int endTag = 0;
while (seek < htmlcode.Length) {
string word = read ();
if (word.ToLower () == "<") {
startTag = seek;
inTag = true;
} else if (word.ToLower () == ">") {
endTag = seek;
inTag = false;
if (iskeepTag (tagName.Replace ("/", ""))) {
for (int i = startTag - 1; i < endTag; i++) {
result.Append (htmlcode[i].ToString ());
}
} else if (tagName.StartsWith ("!--")) {
bool ok = true;
while (ok) {
if (read () == "-") {
if (read () == "-") {
if (read () == ">") {
ok = false;
} else {
seek -= 1;
}
}
}
}
} else {
foreach (string str in specialTag) {
if (tagName == str) {
needContent = false;
break;
} else
needContent = true;
}
}
} else if (!inTag && needContent) {
result.Append (word);
} }
return result.ToString ();
}
/// <summary>
/// 判断是否要保存这个标签
/// </summary>
/// <param name="tag">
/// A <see cref="System.String"/>
/// </param>
/// <returns>
/// A <see cref="System.Boolean"/>
/// </returns>
private bool iskeepTag (string tag)
{
foreach (string ta in keepTag) {
if (tag.ToLower () == ta.ToLower ()) {
return true;
}
}
return false;
}
private string read ()
{
return htmlcode[seek++];
} }
}

  

提取HTML的正文类的更多相关文章

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

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

  2. arcgis python脚本工具实例教程—栅格范围提取至多边形要素类

    arcgis python脚本工具实例教程-栅格范围提取至多边形要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:提取栅格数据的范围, ...

  3. 提取html的正文

    1 using System;  2 using System.Text;  3 namespace HtmlStrip  4 {  5     class MainClass  6     {  7 ...

  4. 提取ecshop的mysql类

    在下一篇文章中,我还将介绍如何完善ecshop的mysql类,使用ecshop的数据库前缀 下载ecshop后,解压缩,进入目录upload/includes,复制里面的cls_mysql.php放进 ...

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

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

  6. VOC2012数据集提取自己需要的类的图片和对应的xml标签

    根据需要修改路径和自己需要的类即可. import os import os.path import shutil fileDir_ann = r'/home/somnus/tttt/VOC2012/ ...

  7. COCO数据集提取特定多个类并在YOLO-V3上训练

    先占个地方,有空再写 ` import os Dir = './coco_class_6/Annotations/val2014' ImageDir = './coco_class_6/images/ ...

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

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

  9. 重构19-Extract Factory Class(提取工厂类)

    在代码中,通常需要一些复杂的对象创建工作,以使这些对象达到一种可以使用的状态.通常情况下,这种创建不过是新建对象实例,并以我们需要的方式进行工作.但是,有时候这种创建对象的需求会极具增长,并且混淆了创 ...

随机推荐

  1. POJ 1734.Sightseeing trip (Floyd 最小环)

    Floyd 最小环模板题 code /* floyd最小环,记录路径,时间复杂度O(n^3) 不能处理负环 */ #include <iostream> #include <cstr ...

  2. [Android分享] 彻底理解ldpi、mdpi、hdpi、xhdpi、xxhdpi

    来自: http://www.eoeandroid.com/thread-565562-1-1.html?_dsign=42bed080 非常感谢楼主分享 这个问题我相信困惑了好多人包括很多老鸟,而且 ...

  3. Linux学习2

    第三讲Linux常用命令04 压缩:gzip: 1.只能压缩文件,不能压缩目录 2.不保留源文件 解压缩:gunzip   gzip -d ------------------------------ ...

  4. sqlserver cte递归向上统计

    数据字典如下

  5. c# 無彈窗调用打印机

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  6. MVC架构模式

    MVC是一个框架模式,它强制性的使应用程序的输入.处理和输出分开.使用MVC应用程序被分成三个核心部件:模型.视图.控制器. V页面传递数据给C,C调用模型处理返回数据给V 最典型的MVC就是JSP ...

  7. 转:Win7 IIS7应用PHP Manager使用FastCGI通道快速部署PHP支持

    原文来自于:http://www.jb51.net/os/windows/62390.html 正常情况下,我们在Windows系统中部署WEB服务器(iis)支持PHP是采用ISAPI通道.参照这篇 ...

  8. windows C 与 linux C区别?

    windows C库格式为  .dll( 动态链接库英文为DLL,是Dynamic Link Library 的缩写形式,DLL是一个包含可由多个程序同时使用的代码和数据的库,DLL不是可执行文件). ...

  9. BZOJ 1057 棋盘制作

    Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴 ...

  10. js/jquery/插件表单验证

    媳妇要学js,就收集一些资料给她. 1.js 表单验证 : http://hi.baidu.com/yanchao0901/item/161f563fb84ea5433075a1eb 2.jquery ...