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. JS实现并集,交集和差集

    var set1 = new Set([1,2,3]);var set2 = new Set([2,3,4]); 并集let union = new Set([...set1, ...set2]); ...

  2. 给某个li标签家样式

    HTML: <div class="tabs clearfix"> <ul id="der"> <li ><a hre ...

  3. Codevs 1010 过河卒== 洛谷 1002

     时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同 ...

  4. 【CF1017B】The Bits(模拟)

    题意:给定两个二进制数字a,b,可以任意交换a中的两个bit位,求进行这样一次操作,最多可产生多少种不同的a or b n<=1e5 思路:模拟,分类讨论 #include<cstdio& ...

  5. C语言的二次实验报告

    题目一 题目二 题目三 题目四 题目五 部分源代码 11-1求矩阵的局部极大值 #include<stdio.h>int main(){    int m,n,i=0,j=0,count= ...

  6. transform与position:fixed的那些恩怨--摘抄

    1. 前言 在写这篇文章之前,我理解的fixed元素是这样的:(摘自CSS布局基础) 固定定位与absolute定位类型类似,但它的相对移动的坐标是视图(屏幕内的网页窗口)本身.由于视图本身是固定的, ...

  7. http协议相关面试题

    浏览器输入url按回车背后经历了哪些? 1.在PC浏览器的地址栏输入一串URL,然后按Enter键这个页面渲染出来,这个过程中都发生了什么事? 1.首先,在浏览器地址栏中输入url,先解析url,检测 ...

  8. [转] iOS应用中通过设置VOIP模式实现休眠状态下socket的长连接

      转自:http://blog.csdn.net/missautumn/article/details/17102067 如果你的应用程序需要在设备休眠的时候还能够收到服务器端发送的消息,那我们就可 ...

  9. Yii命令行模式

    (具体参数描述请使用命令看描述,不过全是英文) 1.Yii提供命令行指令不多,常用的有webapp 和 shell. 1.  message 搜索指定文件信息 yicc message webroot ...

  10. django中表变更后migrate无效的问题

    问题描述: 已有的model,修改之后,想重新建模,于是将migrations文件夹中除__init__.py之外其他文件都删掉,再次执行以下步骤python manage.py makemigrat ...