這是主要對Jsoup 的方法熟悉,Jsoup 學習推薦
下面就是爬取數據的有關代碼:
String link = null; // 書的鏈接
String title = null; // 書的名稱
String score = null; // 書的評分
String num = null ; // 獲取評價人數
Document content = Jsoup.connect("https://book.douban.com/top250").get();
//Elements pl2 = content.getElementsByClass("item");
Elements pl2 = content.select(".item");
int i=1;
for (Element element:pl2){
Element links = element.getElementsByTag("a").first();
Element putureUrl = element.getElementsByTag("img").get(0);
Element star = element.getElementsByClass("star clearfix").get(0);
Element pl21 = element.getElementsByClass("pl2").get(0);
Element controller = element.getElementsByClass("quote").get(0);
link = links.attr("href"); // 獲取電影的鏈接
String url = putureUrl.attr("src");
title = pl21.child(0).html(); // 獲取電影名稱
String content1 = controller.child(0).html(); //簡介
score = star.child(1).html(); // 獲取電影評分
num = star.child(2).html(); // 獲取評價人數
System.out.println(i+++"\t"+link +"\t圖片地址:"+ url+"\t" + title + "\t評分" + score + "\t" + num+"\t"+content1);
}
運行的結果如下:`