提取HTML的正文类
本文转载: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的正文类的更多相关文章
- c# 使用正则表达式 提取章节小说正文全本篇
这一节主要内容是使用正则表达式提取网站的正文,主要面向于小说章节网站.其中涉及到一些其他知识点,比如异步读取.异步流写入等,代码中都会有详细的注解.现在流行的网络文学都是每日一更或几更,没有一个统一的 ...
- arcgis python脚本工具实例教程—栅格范围提取至多边形要素类
arcgis python脚本工具实例教程-栅格范围提取至多边形要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:提取栅格数据的范围, ...
- 提取html的正文
1 using System; 2 using System.Text; 3 namespace HtmlStrip 4 { 5 class MainClass 6 { 7 ...
- 提取ecshop的mysql类
在下一篇文章中,我还将介绍如何完善ecshop的mysql类,使用ecshop的数据库前缀 下载ecshop后,解压缩,进入目录upload/includes,复制里面的cls_mysql.php放进 ...
- 利用正则提取discuz的正文内容
源正文: [p=24, null, left][color=#000][font=宋体]近日,香港著名漫画家马荣成在香港举办的"[color=#ff660][url=http://cul.c ...
- VOC2012数据集提取自己需要的类的图片和对应的xml标签
根据需要修改路径和自己需要的类即可. import os import os.path import shutil fileDir_ann = r'/home/somnus/tttt/VOC2012/ ...
- COCO数据集提取特定多个类并在YOLO-V3上训练
先占个地方,有空再写 ` import os Dir = './coco_class_6/Annotations/val2014' ImageDir = './coco_class_6/images/ ...
- HTML 转文本及HTML内容提取(C#)
//1.HTML直接转文本 //使用方法 HtmlToText convert = new HtmlToText(); textBox2.Text = convert.Convert(textBox1 ...
- 重构19-Extract Factory Class(提取工厂类)
在代码中,通常需要一些复杂的对象创建工作,以使这些对象达到一种可以使用的状态.通常情况下,这种创建不过是新建对象实例,并以我们需要的方式进行工作.但是,有时候这种创建对象的需求会极具增长,并且混淆了创 ...
随机推荐
- 解决办法:CMake编译时出现“error in configuration process project files may be invalid”
无论是CMake2.84 还是当前最新的CMake2.87都可能会出现这种错: 查遍国内外的网上都没有给出可行办法,结果还是自己解决了 现把出错原因和解决办法如下:出错原因:因是英文版本,通常安装没有 ...
- SGU 101.Domino (欧拉路)
时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
- SGU 193.Chinese Girls' Amusement
/* 实际上就是求一个k,满足k<=n/2,且gcd(n,k)=1 如果n为奇数,k为[n/2] 如果n为偶数,k=n/2-1-(n/2)%2 */ #include <iostream& ...
- javascript--”原路返回“
css代码: <style type="text/css"> * { margin: 0px; padding: 0px; font-family: "mic ...
- 第三篇、调优之路 Apache调优
1. 简介 在第一篇中整合了apache + tomcat ,利用了apache解析静态文件为tomcat解压.但是在测试机上发现两者性能不足,不能充分利用服务器的性能,该篇中将对apache进行性 ...
- http拦截器interceptors
在服务里配置$httpProvider.interceptors的相关参数 包含 request请求拦截 response响应拦截 requestError请求错误抛出 responseError响应 ...
- 我摘录的js代码
1.修改样式 document.getElementByIdx( "div1").style.display = "none"; 2.鼠标悬停图标变小手 sty ...
- openshif ssh proxy
最近google又被墙了.没办法 1:注册一个openshift账号.申请注册一个app,获取一个免费主机. https://www.openshift.com/ 2:去PuTTY官方网站下载pL ...
- 测试gcc的优化选项
一.测试准备及原理 测试代码: static void wait(volatile unsigned long dly) { ; dly--); } int main(void) { unsigned ...
- 转:.NET 环境中使用RabbitMQ
原文来自于:http://blog.jobbole.com/83819/ 原文出处: 寒江独钓 欢迎分享原创到伯乐头条 在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统 ...