背景:
获取html页面所有有text节点的xpath
思路:
NekoHTML是一个Java语言的 HTML扫描器和标签补全器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。而dom4j可以很好的用于XML,XPath和XSL。
基于html构建xml
基于xml获取所有的有text的xpath
环境:
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.10</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
实现如下:
import java.io.FileInputStream; import java.util.Iterator; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.cyberneko.html.parsers.DOMParser; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.DOMReader; import org.xml.sax.InputSource; public class NekoHTML { public static void main(String[] args) throws Throwable { DOMParser parser = new DOMParser(); parser.parse(new InputSource(new FileInputStream("D:/dom/1.htm"))); DOMReader domReader = new DOMReader(); Document document = domReader.read(parser.getDocument()); Element root = document.getRootElement(); Map<String, String> map = new java.util.concurrent.ConcurrentHashMap<String, String>(); dom2XPathMap(root, map); System.out.println(map); } private static void dom2XPathMap(Element root, Map<String, String> map) { if (root == null || root.isTextOnly()) { if (StringUtils.isNotEmpty(root.getText())) { map.put(root.getUniquePath(), root.getText()); } if (map.containsKey(root.getUniquePath())) { } return; } Iterator<Element> iterator = root.elementIterator(); while (iterator.hasNext()) { Element el = iterator.next(); dom2XPathMap(el, map); } } }
相关推荐
采用nekohtml补全html到xhtml,结合dom,运用dom4j,支持xpath,强大的网抓工具!
NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的...
NekoHTML的相关用法以及代码包,以及详细的用法和实例。。。。。
NekoHTML技术预研说明,如何进行HTML页面解析,网页信息抽取
NekoHTML is a simple HTML scanner and tag balancer that enables application programmers to parse HTML documents and access the information using standard XML interfaces. The parser can scan HTML files...
nekoHtml 1.9.19 加 source 源码 html分析jar
NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的...
NekoHTML是一个Java语言的 HTML扫描器和标签补全器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能够扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档 过程中常...
html解析器nekohtml-1.9.13.zip
如果为false,<html>和被忽略,所有的内容都被解析。 http://cyberneko.org/html/features/document-fragment False 解析HTML片段时是否作标签增补。此功能不要用在DOMParser上,而要用在DOMFragmentParser上。 ...
NULL 博文链接:https://thrillerzw.iteye.com/blog/1924229
NULL 博文链接:https://rensanning.iteye.com/blog/1551831
NekoHTML学习笔记.doc
nekohtml.jar 解析html的jar包 非常的实用
NeKoHTML:用于java的html编辑。此文件是NeKoHTML V1.9.21版本的Java包合集。
可以解析html的包,和例子,有源代码,很简单,不明白请看压缩包里的readme
nekohtml.jar nekohtmlSamples.jar
NULL 博文链接:https://fhqiwcw.iteye.com/blog/1462239
nekohtml-1.9.14源码及jar包
NULL 博文链接:https://tivonhou.iteye.com/blog/2204060