但是不是纯java的解决方案.于是到处搜索,在网上找到了一个htmlparser.
过几天贴出lucene进行全文检索的代码.(检索本站的文章等).
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.inputstreamreader;
import org.htmlparser.node;
import org.htmlparser.nodefilter;
import org.htmlparser.parser;
import org.htmlparser.filters.nodeclassfilter;
import org.htmlparser.filters.orfilter;
import org.htmlparser.nodes.textnode;
import org.htmlparser.tags.linktag;
import org.htmlparser.util.nodelist;
import org.htmlparser.util.parserexception;
import org.htmlparser.visitors.htmlpage;
import org.htmlparser.visitors.textextractingvisitor;
import com.jscud.util.logman; //一个日志记录类
/**
* 演示了html parse的应用.
*
* @author scud http://www.jscud.com
*/
public class parsehtmltest
{
public static void main(string[] args) throws exception
{
string afile = e:/jscud/temp/test.htm;
string content = readtextfile(afile, gbk);
test1(content);
system.out.println(====================================);
test2(content);
system.out.println(====================================);
test3(content);
system.out.println(====================================);
test4(content);
system.out.println(====================================);
test5(afile);
system.out.println(====================================);
//访问外部资源,相对慢
test5(http://www.jscud.com);
system.out.println(====================================);
}
/**
* 读取文件的方式来分析内容.
* filepath也可以是一个url.
*
* @param resource 文件/url
*/
public static void test5(string resource) throws exception
{
parser myparser = new parser(resource);
//设置编码
myparser.setencoding(gbk);
htmlpage visitor = new htmlpage(myparser);
myparser.visitallnodeswith(visitor);
string textinpage = visitor.gettitle();
system.out.println(textinpage);
}
/**
* 按页面方式处理.对一个标准的html页面,推荐使用此种方式.
*/
public static void test4(string content) throws exception
{
parser myparser;
myparser = parser.createparser(content, gbk);
htmlpage visitor = new htmlpage(myparser);
myparser.visitallnodeswith(visitor);
string textinpage = visitor.gettitle();
system.out.println(textinpage);
}
/**
* 利用visitor模式解析html页面.
*
* 小优点:翻译了<>等符号
* 缺点:好多空格,无法提取link
*
*/
public static void test3(string content) throws exception
{
parser myparser;
myparser = parser.createparser(content, gbk);
textextractingvisitor visitor = new textextractingvisitor();
myparser.visitallnodeswith(visitor);
string textinpage = visitor.getextractedtext();
system.out.println(textinpage);
}
/**
* 得到普通文本和链接的内容.
*
* 使用了过滤条件.
*/
public static void test2(string content) throws parserexception
{
parser myparser;
nodelist nodelist = null;
myparser = parser.createparser(content, gbk);
nodefilter textfilter = new nodeclassfilter(textnode.class);
nodefilter linkfilter = new nodeclassfilter(linktag.class);
//暂时不处理 meta
//nodefilter metafilter = new nodeclassfilter(metatag.class);
orfilter lastfilter = new orfilter();
lastfilter.setpredicates(new nodefilter[] { textfilter, linkfilter });
nodelist = myparser.parse(lastfilter);
node[] nodes = nodelist.tonodearray();
for (int i = 0; i < nodes.length; i++)
{
node anode = (node) nodes[i];
string line = "";
if (anode instanceof textnode)
{
textnode textnode = (textnode) anode;
//line = textnode.toplaintextstring().trim();
line = textnode.gettext();
}
else if (anode instanceof linktag)
{
linktag linknode = (linktag) anode;
line = linknode.getlink();
//@todo 过滤jsp标签:可以自己实现这个函数
//line = stringfunc.replace(line, "<%.*%>, );
}
if (istrimempty(line))
continue;
system.out.println(line);
}
}
/**
* 解析普通文本节点.
*
* @param content
* @throws parserexception
*/
public static void test1(string content) throws parserexception
{
parser myparser;
node[] nodes = null;
myparser = parser.createparser(content, null);
nodes = myparser.extractallnodesthatare(textnode.class); //exception could be thrown here
for (int i = 0; i < nodes.length; i++)
{
textnode textnode = (textnode) nodes[i];
string line = textnode.toplaintextstring().trim();
if (line.equals())
continue;
system.out.println(line);
}
}
/**
* 读取一个文件到字符串里.
*
* @param sfilename 文件名
* @param sencode string
* @return 文件内容
*/
public static string readtextfile(string sfilename, string sencode)
{
stringbuffer sbstr = new stringbuffer();
try
{
file ff = new file(sfilename);
inputstreamreader read = new inputstreamreader(new fileinputstream(ff),
sencode);
bufferedreader ins = new bufferedreader(read);
string dataline = ;
while (null != (dataline = ins.readline()))
{
sbstr.append(dataline);
sbstr.append(\r\n);
}
ins.close();
}
catch (exception e)
{
logman.error(read text file error, e);
}
return sbstr.tostring();
}
/**
* 去掉左右空格后字符串是否为空
* @param astr string
* @return boolean
*/
public static boolean istrimempty(string astr)
{
if ((null == astr) || (astr.length() == 0))
{
return true;
}
if (isblank(astr.trim()))
{
return true;
}
return false;
}
/**
* 字符串是否为空:null或者长度为0.
* @param astr 源字符串.
* @return boolean
*/
public static boolean isblank(string astr)
{
if ((null == astr) || (astr.length() == 0))
{
return true;
}
else
{
return false;
}
}
}
以上就是htmlparser使用教程的内容。
