java解析html
package com.list;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import com.yao.http.HttpRequester;
import com.yao.http.HttpRespons;
/**
* JAVA中使用Htmlparse解析HTML文档,使用htmlparse遍历出HTML文档的所有超链接(<a>标记)。
*
* @author YYmmiinngg 转自其他网站,地址未知
*/
public class bianli {
public static void main(String[] args) {
try {
HttpRequester request = new HttpRequester();
HttpRespons hr = request
.sendGet("http://sourceforge.net/p/xamj/patches/43/");
Parser parser = Parser.createParser(hr.getContent(),
hr.getContentEncoding());
try {
// 通过过滤器过滤出<A>标签
NodeList nodeList = parser
.extractAllNodesThatMatch(new NodeFilter() {
private static final long serialVersionUID = 1L;
public boolean accept(Node node) {
// 此处过滤出来 LinkTag ,此处可以过滤任何标签 例如div table
if (node instanceof LinkTag)
return true;
return false;
}
});
for (int i = 0; i < nodeList.size(); i++) {
LinkTag n = (LinkTag) nodeList.elementAt(i);
System.out.println(n.extractLink());
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}