public class TestData { /*** * 抓取数据的代码 */ public static void main(String[] args) { try { // 1.创建html解析器 Parser p = new Parser(); // 2.设置需要解析的html URL路径 p.setURL("http://zhushou.360.cn/list/index/cid/1?page=1"); // 3.只获得li节点 // 3.1.创建一个li节点的过滤器 HasAttributeFilter haf = new HasAttributeFilter("id", "iconList"); HasParentFilter hpf = new HasParentFilter(haf); AndFilter af = new AndFilter(hpf, new TagNameFilter("li")); // 3.2.通过li过滤器获得所有li节点 NodeList nodeList = p.parse(af); StringBuffer sb = new StringBuffer(); sb.append("@["); for (int i = 0; i < nodeList.size(); i++) { // 4.1.获得了对应li节点 TagNode liNode = (TagNode) nodeList.elementAt(i); // 4.2.获得li里面的图片(img节点) NodeList liChildren = liNode.getChildren(); TagNode imgNode = (TagNode) liChildren.elementAt(0).getChildren().elementAt(0); String url = imgNode.getAttribute("_src"); // 4.3.获得软件名称 TextNode nameNode = (TextNode) liChildren.elementAt(1).getChildren().elementAt(0).getChildren().elementAt(0); String name = nameNode.getText(); // 4.4.获得软件的下载量 TextNode downloadNode = (TextNode) liChildren.elementAt(2).getChildren().elementAt(0); String download = downloadNode.getText(); if (i != 0) { sb.append(" , "); } sb.append("@{"); sb.append("@\"icon\" : @\"" + url + "\","); sb.append("@\"name\" : @\"" + name + "\","); sb.append("@\"download\" : @\"" + download + "\""); sb.append("}"); } sb.append("]"); System.out.println(sb); /* @[ @{ @"icon" : @"ggg.png", @"name" : @"360手机卫士", @"download" : @"65666次下载" }, @{ @"icon" : @"ggg.png", @"name" : @"360手机卫士", @"download" : @"65666次下载" }, @{ @"icon" : @"ggg.png", @"name" : @"360手机卫士", @"download" : @"65666次下载" } ]*/ } catch (Exception e) { e.printStackTrace(); } } }
转载于:https://www.cnblogs.com/changxs/p/3484925.html