import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.beans.StringBean;
import org.htmlparser.filters.CssSelectorNodeFilter;
import org.htmlparser.util.NodeList; public class HtmlUtil {
public static String getText(String html, String id) {
try {
Parser parser = new Parser(html);
NodeFilter filter = new CssSelectorNodeFilter("#" + id);
NodeList nList = parser.extractAllNodesThatMatch(filter);
return nList == null || nList.size() == 0 ? null : nList.elementAt(
0).toPlainTextString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static String getTextByClass(String html, String css_class) {
try {
Parser parser = new Parser(html);
NodeFilter filter = new CssSelectorNodeFilter("." + css_class);
NodeList nList = parser.extractAllNodesThatMatch(filter);
return nList == null || nList.size() == 0 ? null : nList.elementAt(
0).toPlainTextString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static String filterText(String text) {
if (text == null)
return null;
text = text.replace(">", ">");
text = text.replace("<", "<");
text = text.replace(""", "\"");
text = text.replace(" ", " ");
text = text.replace("&", "&");
text = text.replace("&copy;", "©");
text = text.replace(" ", "");
return text;
} /**
* 获取网页中纯文本信息
*
* @param html
* @param id
* @return
* @throws Exception
* @throws Exception
*/
public static String getText(String html) throws Exception {
StringBean bean = new StringBean();
bean.setLinks(false);
bean.setReplaceNonBreakingSpaces(true);
bean.setCollapse(true); // 返回解析后的网页纯文本信息
Parser parser = Parser.createParser(html, "utf-8");
parser.visitAllNodesWith(bean);
parser.reset();
return bean.getStrings();
}
}

须要用htmlparse.jar库,调用方式例如以下:

HtmlUtil.getText(htmlStr)。

html抽取文本信息-java版(适合lucene建立索引)的更多相关文章

  1. Lucene建立索引搜索入门实例

                                第一部分:Lucene建立索引 Lucene建立索引主要有以下两步:第一步:建立索引器第二步:添加索引文件准备在f盘建立lucene文件夹,然后 ...

  2. 【转】Lucene不同版本中Field的Keyword、UnIndex,导致lucene 建立索引总是报错 急!!

    lucene 建立索引 总是报错 急!! http://zhidao.baidu.com/link?url=iaVs9JH4DfN6iwaWImt7VMJENWCWGGaWFGPjqhUw_jz7Fs ...

  3. Lucene4.9学习笔记——Lucene建立索引

    基本上创建索引需要三个步骤: 1.创建索引库IndexWriter对象 2.根据文件创建文档Document 3.向索引库中写入文档内容 这其中主要涉及到了IndexWriter(索引的核心组件,用于 ...

  4. lucene 建立索引的过程

    时间 -- ::  CSDN博客 原文 http://blog.csdn.net/caohaicheng/article/details/ 看lucene主页(http://lucene.apach ...

  5. lucene建立索引的过程

    建立索引过程 用户提交数据=>solr建立索引=>调用lucene包建立索引 官方建立索引和查询索引的例子如下: http://lucene.apache.org/core/4_10_3/ ...

  6. lucene 建立索引的不同方式

    1.创建一个简单的索引: package lia.meetlucene; import java.io.File; import org.apache.lucene.document.Document ...

  7. Lucene实现索引和查询

    0引言 随着万维网的发展和大数据时代的到来,每天都有大量的数字化信息在生产.存储.传递和转化,如何从大量的信息中以一定的方式找到满足自己需求的信息,使之有序化并加以利用成为一大难题.全文检索技术是现如 ...

  8. 利用Lucene将被索引文件目录中的所有文件建立索引

    1.新建两个文件夹htm和index,其中htm中存放被索引的文件,index文件中存放建立的索引文件. 2.新建解析目录中所有文件的类,用来解析指定目录下的所有文件. import java.io. ...

  9. 关于富文本编辑器—UEditor(java版)的使用,以及如何将UEditor的文件/图片上传路径改成绝对路径

    突然发现好久没写博客了,感觉变懒了,是要让自己养成经常写文章的习惯才行.既可以分享自己的所学,和所想,和大家一起讨论,发现自己的不足的问题. 大家可能经常会用到富文本编辑器,今天我要说的是UEdito ...

随机推荐

  1. 【python自动化】python 常用时间获取方法

    代码如下: import datetime import time DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" DATE_FORMAT = &quo ...

  2. hdu 1087 最大上升子序列的和(dp或线段树)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. es6总结(六)--新数据类型-Symbol

  4. 转 Django+Bootstrap练习--我的类博客系统开发

    转自: http://blog.sina.com.cn/s/blog_7e050dc80102w312.html 本文记录了一个类博客网站从无到有的搭建过程,同时也是我入门django以及再次入门前端 ...

  5. linux信号-------初涉

    一.信号的本质 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.在软件层次上是对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是进程 ...

  6. configure: error: off_t undefined; check your library configuration

    configure: error: off_t undefined; check your library configuration 发生背景: 编译PHP时出现的提示,报错信息为: configu ...

  7. hdu 3605 /状态合并最大流

    题意:N个人去m个星球,给出n个人可以去哪些星球的01矩阵.求是否能满足所有人都去.(n到10万,m<=10) 一看,起先一瞬间就建图,准备秒了,人向星球连边,直接最大流判断是否为n,提交超时. ...

  8. http错误种类及原因

    http://blog.csdn.net/dxykevin/article/details/50950878 [摘要]HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应 ...

  9. eclipse启动的时候 一直未响应状态 然后闪退

    解决方案:在你的工作目录中,有一个.metadata目录,里面是工作区及各插件的信息,删除此目录可以解决问题.保险起见,先剪切到另一地方.再重启启动.

  10. [Python Cookbook] Numpy Array Manipulation

    1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...