HtmlParser解析HTML

  在抓取完爬虫数据后就要提取我们需要的内容了,这时就要用到htmlparser对HTML标签进行解析,如对Image.Link,Iframe地址等。
   版本 HtmlParser 2.0

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.PrototypicalNodeFactory;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.tags.FrameTag;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class OrFilterForIamgeOrLinkOrFrame {

public static void main(String[] args) throws ParserException {
getImage("http://dataprice.jcauto.com.cn/ProConsole/carComments/pShowCarCompareThree.action?ids=01400010000001,01400009000001&zj=121");
}

public static void getImage(String url) throws ParserException {
Parser parser = new Parser(url);
parser.setEncoding("gb2312");
PrototypicalNodeFactory pnfPrototypicalNodeFactory = new PrototypicalNodeFactory();
pnfPrototypicalNodeFactory.registerTag(new IframeTag());
parser.setNodeFactory(pnfPrototypicalNodeFactory);

NodeFilter filter = new NodeClassFilter(ImageTag.class);
NodeFilter filter1 = new NodeClassFilter(LinkTag.class);
NodeFilter filter2 = new NodeClassFilter(IframeTag.class);
OrFilter orFilter = new OrFilter(filter1, filter);
OrFilter orFilter1 = new OrFilter(filter2, orFilter);
NodeList nodelist = parser.extractAllNodesThatMatch(orFilter1);
for (Node node : nodelist.toNodeArray()) {
if (node instanceof ImageTag) {
ImageTag img = (ImageTag) node;
if (img != null) {
System.out
.println("imag------------->" + img.getImageURL());

}
}
if (node instanceof LinkTag) {
LinkTag link = (LinkTag) node;
if (link != null) {
System.out.println("herf------------->" + link.getLink());
}
}
if (node instanceof IframeTag) {
{
IframeTag frame = (IframeTag) node;

if (frame != null) {
System.out.println("iframe------------->"+ frame.getAttribute("src"));
}

}
}
}

}
}


对多标签处理就要采用OrFilter,必须要注意的是配置OrFilter时只有new PrototypicalNodeFactory ,然后将IframeTag标签放置在registerTag里,在将对象setNodeFactory里,orFilter才会起作用,否则访问不到IFRAME的内容。

你可能感兴趣的:(html)