`

NekoHtml解析 html 文件

阅读更多
最近做了一个 html 解析的 小项目,用的是 NekoHtml, 解析静态的html页面,提取需要的信息 成jason对象并放到一个文件中。

思路是, 先使用Netko可以快速的将需要的html中的指定标签如 table 中的信息拿到。 然后存入临时生成一个temp.html页面,再重新解析成 node对象。就可以根据结构获得制定的 node下的内容了。


核心代码如下:

public static CustomerRecord convertFileToObj(String filePath) throws Exception {
		CustomerRecord cr = new CustomerRecord();
		List<SOARec> soaList = new ArrayList<SOARec>();
		List<ARec> aList = new ArrayList<ARec>();
		List<MXRec> mxList = new ArrayList<MXRec>();
		List<NSRec> nxList = new ArrayList<NSRec>();
		
		// *Get Need Content from file
		File file = new File(filePath);
		cr.setFileName(file.getName());
		// create element remover filter
		ElementRemover remover = new ElementRemover();
		// set which elements to accept
		remover.acceptElement("table", null);
		remover.acceptElement("td", null);
		remover.acceptElement("tr", null);
		remover.removeElement("title");
		StringWriter filteredDescription = new StringWriter();
		// create writer filter
		org.cyberneko.html.filters.Writer writer = new org.cyberneko.html.filters.Writer(filteredDescription, null);
		// setup filter chain
		XMLDocumentFilter[] filters = { remover, writer, };
		// create HTML parser
		XMLParserConfiguration parser = new HTMLConfiguration();
		parser.setProperty("http://cyberneko.org/html/properties/filters", filters);
		XMLInputSource source = new XMLInputSource(null, filePath, null);
		parser.parse(source);
		String description = filteredDescription.toString();
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
		Matcher m = p.matcher(description);
		description = m.replaceAll("");

		// * wirte the content into file
		File temp = new File(file.getParentFile().getPath(), "temp.html");
		Writer out = null;
		out = new FileWriter(temp, false);
		out.write(description);
		out.close();

		DOMParser parser2 = new DOMParser();
		parser2.parse(temp.getPath());
		Document document = parser2.getDocument();
		int a = 0;

		NodeList nodeList = XPathAPI.selectNodeList(document, "//TR");
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i);
			String trContent = node.getTextContent();
			//System.out.println(trContent);
			
			//Start to convent into object.
			// Domain
			if (trContent.equals("ZoneundRecordsbearbeiten")) {
				a = i;
			}
		
			if (i == (a + 2)) {
				if (trContent.contains("Domain")) {
					cr.setDomain(node.getChildNodes().item(1).getTextContent());
				}
			}
			// SOA
			if (trContent.startsWith("SOARecord")) {
				NodeList soanodes = node.getChildNodes().item(1).getChildNodes().item(0).getChildNodes();
				for (int j = 1; j < soanodes.getLength(); j++) {
					SOARec soa = new SOARec();
					soa.setDomain(soanodes.item(j).getChildNodes().item(0).getTextContent());
					soa.setSeriennummer(soanodes.item(j).getChildNodes().item(1).getTextContent());
					soa.setEmail(soanodes.item(j).getChildNodes().item(2).getTextContent());
					soa.setPrimaryDNS(soanodes.item(j).getChildNodes().item(3).getTextContent());
					soaList.add(soa);
				}
			}
			// A
			if (trContent.startsWith("ARecords")) {
				NodeList anodes = node.getChildNodes().item(1).getChildNodes().item(0).getChildNodes();
				for (int j = 1; j < anodes.getLength(); j++) {
					ARec ar = new ARec();
					ar.setHost(anodes.item(j).getChildNodes().item(0).getTextContent());
					ar.setIp(anodes.item(j).getChildNodes().item(1).getTextContent());
					ar.setTtl(anodes.item(j).getChildNodes().item(2).getTextContent());
					aList.add(ar);
				}
			}
			// MXRecords
			if (trContent.startsWith("MXRecords")) {
				NodeList mxnodes = node.getChildNodes().item(1).getChildNodes().item(0).getChildNodes();
				for (int j = 1; j < mxnodes.getLength(); j++) {
					MXRec mx = new MXRec();
					mx.setHost(mxnodes.item(j).getChildNodes().item(0).getTextContent());
					mx.setMailExchanger(mxnodes.item(j).getChildNodes().item(1).getTextContent());
					mx.setTtl(mxnodes.item(j).getChildNodes().item(2).getTextContent());
					mx.setPreference(mxnodes.item(j).getChildNodes().item(3).getTextContent());
					mxList.add(mx);
				}
			}
			// NSRecords
			if (trContent.startsWith("NSRecords")) {
				NodeList nsnodes = node.getChildNodes().item(1).getChildNodes().item(0).getChildNodes();
				for (int j = 1; j < nsnodes.getLength(); j++) {
					NSRec ns = new NSRec();
					ns.setHost(nsnodes.item(j).getChildNodes().item(0).getTextContent());
					ns.setNameserver(nsnodes.item(j).getChildNodes().item(1).getTextContent());
					ns.setTtl(nsnodes.item(j).getChildNodes().item(2).getTextContent());
					nxList.add(ns);
				}
			}

		}
		cr.setaRecList(aList);
		cr.setMxRecList(mxList);
		cr.setSoaRecList(soaList);
		cr.setNxRecList(nxList);
		temp.delete();
		return cr;
	}
分享到:
评论

相关推荐

    nekohtml包能够解析HTML文件

    NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的...这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的错误。

    nekohtml解析器

    NekoHTML是一个Java语言的 HTML扫描器和标签补全器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问...这个解析器能够扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档 过程中常犯的错误。

    html解析例子,用nekohtml写的

    可以解析html的包,和例子,有源代码,很简单,不明白请看压缩包里的readme

    NekoHtml 解析内容时需要注意的地方

    NULL 博文链接:https://tianhewulei.iteye.com/blog/629704

    NekoHTML学习笔记.rar

    这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的错误。NekoHTML能增补缺失的父元素、自动用结束标签关闭相应的元素,以及不匹配的内嵌元素标签。NekoHTML的开发使用了Xerces ...

    Java解析HTML之NekoHTML

    NULL 博文链接:https://rensanning.iteye.com/blog/1551831

    NekoHTML技术

    NekoHTML技术预研说明,如何进行HTML页面解析,网页信息抽取

    nekohtml-1.9.13.zip

    html解析器nekohtml-1.9.13.zip

    NekoHTML校验HTML

    http://cyberneko.org/html/properties/default-encoding Windows-1252 IANA encoding names 默认的HTML文件编码 http://cyberneko.org/html/properties/names/elems upper upper,lower,match 如果整理识别出的元素...

    nekohtml.jar

    nekohtml.jar 解析html的jar包 非常的实用

    nekohtml-1.9.15.zip

    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

    NULL 博文链接:https://thrillerzw.iteye.com/blog/1924229

    nekoHtml 1.9.19 加 source 源码 html分析jar

    nekoHtml 1.9.19 加 source 源码 html分析jar

    NeKoHTML 1.9.21

    NeKoHTML:用于java的html编辑。此文件是NeKoHTML V1.9.21版本的Java包合集。

    NekoHTML学习笔记.doc

    NekoHTML学习笔记.doc

    nekohtml+dom4j

    采用nekohtml补全html到xhtml,结合dom,运用dom4j,支持xpath,强大的网抓工具!

    nekohtml.jar-nekohtml

    nekohtml.jar nekohtmlSamples.jar

    NekoHTML的相关用法以及j代码包

    NekoHTML的相关用法以及代码包,以及详细的用法和实例。。。。。

    nekohtml-1.9.14源码及jar包

    nekohtml-1.9.14源码及jar包

Global site tag (gtag.js) - Google Analytics