HTMLParser获取属性名方式:
原始网页文本:
<a title="美军被曝虐尸" href="http://www.sogou.com/web?query=%C3%C0%BE%FC%B1%BB%C6%D8%C5%B0%CA%AC" target="_blank">美军被曝虐尸</a></li><li><strong class="num2">2</strong><a title="温岭鞋厂大火" href="http://www.sogou.com/web?query=%CE%C2%C1%EB%D0%AC%B3%A7%B4%F3%BB%F0" target="_blank">温岭鞋厂大火</a></li><li><strong class="num2">3</strong><a title="2014春运全国启动" href="http://www.sogou.com/web?query=2014%B4%BA%D4%CB%C8%AB%B9%FA%C6%F4%B6%AF" target="_blank">2014春运全国启动</a></li><li><strong class="num2">4</strong><a title="汪峰章子怡同居" href="http://www.sogou.com/web?query=%CD%F4%B7%E5%D5%C2%D7%D3%E2%F9%CD%AC%BE%D3" target="_blank">汪峰章子怡同居</a></li><li><strong class="num2">5</strong><a title="透视年会" href="http://www.sogou.com/web?query=%CD%B8%CA%D3%C4%EA%BB%E1" target="_blank">透视年会</a></li>
经过parser处理后文本:
parser匹配代码:
NodeFilter filter1 = new HasAttributeFilter("a");
NodeFilter filter2 = new HasAttributeFilter("title"); //NodeFilter innerFilter = new TagNameFilter ("title");
//NodeFilter filter2 = new HasChildFilter(innerFilter);
NodeFilter filter = new AndFilter(filter1, filter2);
NodeList nodes = parser.extractAllNodesThatMatch(filter);
处理后文本:
<a title="美军被曝虐尸" href="http://www.sogou.com/web?query=%C3%C0%BE%FC%B1%BB%C6%D8%C5%B0%CA%AC" target="_blank">美军被曝虐尸</a>
<a title="温岭鞋厂大火" href="http://www.sogou.com/web?query=%CE%C2%C1%EB%D0%AC%B3%A7%B4%F3%BB%F0" target="_blank">温岭鞋厂大火</a>
<a title="2014春运全国启动" href="http://www.sogou.com/web?query=2014%B4%BA%D4%CB%C8%AB%B9%FA%C6%F4%B6%AF" target="_blank">2014春运全国启动</a>
<a title="汪峰章子怡同居" href="http://www.sogou.com/web?query=%CD%F4%B7%E5%D5%C2%D7%D3%E2%F9%CD%AC%BE%D3" target="_blank">汪峰章子怡同居</a>
<a title="透视年会" href="http://www.sogou.com/web?query=%CD%B8%CA%D3%C4%EA%BB%E1" target="_blank">透视年会</a>
得到属性名:
也就是下方的标签属性:
匹配代码:
Node node = (Node)nodes.elementAt(i);
LinkTag linkTag = (LinkTag)node;
System.out.println(linkTag.getAttribute("title"));
匹配结果:
美军被曝虐尸
温岭鞋厂大火
2014春运全国启动
汪峰章子怡同居
透视年会
官方API:http://htmlparser.sourceforge.net/javadoc/