需求分析
获取服务端的数据 以ListView显示出来 URL地址 http://book.douban.com/latest
话不多说 先上代码
Java工程
TestNewBook.java
public class TestNewBook {
public static void main(String[] args) throws Exception{
String path ="http://book.douban.com/latest";
URL url = new URL(path);
URLConnection conn = url.openConnection();
Source source = new Source(conn);
List<Element> lielements = source.getAllElements("li");
System.out.println(lielements.size());
for(Element lielement : lielements){
List<Element> childrenlists = lielement.getChildElements();
if(childrenlists.size()==2){
if("detail-frame".equals(childrenlists.get(0).getAttributeValue("class"))){
//数目对应的div信息
Element div = childrenlists.get(0);
List<Element> divChildren = div.getChildElements();
String name = divChildren.get(0).getTextExtractor().toString();
System.out.println("书名:"+name);
String description = divChildren.get(1).getTextExtractor().toString();
System.out.println("描述: "+description);
String summary = divChildren.get(2).getTextExtractor().toString();
System.out.println("简介: "+summary);
Element achild = childrenlists.get(1);
String iconpath = achild.getChildElements().get(0).getAttributeValue("src");
System.out.println("图片地址 : "+iconpath);
System.out.println("---------------------");
}
}
}
}
}
结果
代码分析
需要导入的外表Jar包
jericho-html-3.1.jar
解析HTML