import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.URL;
import org.cyberneko.html.parsers.DOMParser;
import org.htmlparser.tags.Span;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class JavaTest {
public static String TextExtractor(Node root){
//若是文本节点的话,直接返回
if (root.getNodeType() == Node.TEXT_NODE) {
return "";
}
if(root.getNodeType() == Node.ELEMENT_NODE) {
Element elmt = (Element) root;
//抛弃脚本
if (elmt.getTagName().equals("STYLE")
|| elmt.getTagName().equals("SCRIPT"))
return "";
NodeList children = elmt.getChildNodes();
StringBuilder text = new StringBuilder();
if (elmt.getTagName().equals("SPAN")) {
if (elmt.hasAttribute("id")) {
// System.out.println(elmt.getAttribute("id"));
if (elmt.getAttribute("id").equals("countOfPrd")) {
return children.item(0).getNextSibling().getNextSibling().getNextSibling().getFirstChild().getNodeValue();
}
}
}
for (int i = 0; i < children.getLength(); i++) {
text.append(TextExtractor(children.item(i)));
}
return text.toString();
}
//对其它类型的节点,返回空值
return "";
}
public static void main(String[] args) throws Exception{
//生成html parser
DOMParser parser = new DOMParser();
//设置网页的默认编码
parser.setProperty(
"http://cyberneko.org/html/properties/default-encoding",
"utf-8");
//input file
URL a = new URL("http://www.suning.cn/webapp/wcs/stores/servlet/prd_10052_10051_-7_9173_196583_.html");
BufferedReader in = new BufferedReader(new InputStreamReader(a.openStream()));
//BufferedReader in = new BufferedReader(new FileReader("input.htm"));
parser.parse(new InputSource(in));
Document doc = parser.getDocument();
//获得body节点,以此为根,计算其文本内容
Node body = doc.getElementsByTagName("BODY").item(0);
System.out.println(TextExtractor(body));
}
}
//该段代码是用来解析抓取各大网站的价格,来智能化定价。实现价格战的目的赢得用户,不懂或更多探究QQ526151410
分享到:
相关推荐
NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的...
NekoHTML是一个Java语言的 HTML扫描器和标签补全器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能够扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档 过程中常...
NULL 博文链接:https://tivonhou.iteye.com/blog/2204060
NULL 博文链接:https://tianhewulei.iteye.com/blog/629704
可以解析html的包,和例子,有源代码,很简单,不明白请看压缩包里的readme
NekoHTML技术预研说明,如何进行HTML页面解析,网页信息抽取
NekoHTML是一个简单地HTML扫描器和标签补偿器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。这个解析器能投扫描HTML文件并“修正”许多作者(人或机器)在编写HTML文档过程中常犯的...
NULL 博文链接:https://rensanning.iteye.com/blog/1551831
html解析器nekohtml-1.9.13.zip
NULL 博文链接:https://thrillerzw.iteye.com/blog/1924229
nekohtml.jar 解析html的jar包 非常的实用
NekoHTML is written using the Xerces Native Interface (XNI) that is the foundation of the Xerces2 implementation. This enables you to use the NekoHTML parser with existing XNI tools without ...
NekoHTML学习笔记.doc
NekoHTML的相关用法以及代码包,以及详细的用法和实例。。。。。
NeKoHTML:用于java的html编辑。此文件是NeKoHTML V1.9.21版本的Java包合集。
nekohtml.jar nekohtmlSamples.jar
nekohtml-1.9.14源码及jar包
采用nekohtml补全html到xhtml,结合dom,运用dom4j,支持xpath,强大的网抓工具!
nekoHtml 1.9.19 加 source 源码 html分析jar
nekohtml-1.9.18_