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