抓取的是我博客的文章標題和鏈接阅嘶,使用map存儲,直接附上代碼:
/*
* 時間:2016年十月
* 作者:劉袆飛
* 一個練習,使用jsoup獲取我的簡書的所有文章題目和鏈接信息
*/
package jsoup.com;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;
import java.util.*;
public class Main {
public static void main(String[] args){
try {
//申請兩個元素集合鲁驶,title存儲獲取到的分頁數(shù)據(jù)俱两,link存儲UI中的數(shù)據(jù)
Elements title = null;
Elements link = new Elements();
//存儲獲取到的數(shù)據(jù),<鏈接作為KEY痛侍,text作為值>
Map<String,String> map = new HashMap<>();
for(int i =0;;i++){
Document doc = Jsoup.connect("http://www.reibang.com/users/d2365eff6e41/latest_articles?page="+ i ).get();
title = doc.getElementsByTag("h4");
if(title.size() != 0){
Iterator<Element> it = title.iterator();
while(it.hasNext()){
link.add(it.next().child(0));
}
title=null;
}else
break;
}
//吧元素集合里面的數(shù)據(jù)存儲到map中朝氓,鏈接取完整地址
Iterator<Element> it = link.iterator();
while(it.hasNext()){
Element temp = it.next();
map.put(temp.attr("abs:href"),temp.text());
}
System.out.println(map);
} catch (Exception e) {
System.out.println(e);
}
}
}
輸出如下:
{http://www.reibang.com/p/4502b2802cf8=ArrayList練習-去掉重復元素,
http://www.reibang.com/p/5565e5c59afd=單表替換密碼,
http://www.reibang.com/p/f23ec9da6ecf=java集合,
http://www.reibang.com/p/655c2ea168b0=面向對象練習二,
http://www.reibang.com/p/0eb7a2030b6d=java集合-Set,
http://www.reibang.com/p/56c9bcc713a2=Killer網(wǎng)卡未正確映射ISP問題解決辦法,
http://www.reibang.com/p/9e5d38c36373=蒙塔卡羅法計算π,
http://www.reibang.com/p/58b92b4a428a=維吉尼亞加密遇到的問題,
http://www.reibang.com/p/42b914293435=重裝Win7后無法獲取更新解決辦法,
http://www.reibang.com/p/6c65417f1237=我的鍵盤,
http://www.reibang.com/p/ad52b1207a9a=國慶寫代碼,
http://www.reibang.com/p/d9141fccc345=驗證哥德巴赫猜想,
http://www.reibang.com/p/b3d9c603d2c8=Comparator練習-String按照長度比較,
http://www.reibang.com/p/aba38279d14f=Oracle數(shù)據(jù)庫學習整理,
http://www.reibang.com/p/d0505d11c819=巧用windows任務欄,
http://www.reibang.com/p/5af00815b4e4=vim基本操作命令,
http://www.reibang.com/p/83d2f2f8fff5=維吉尼亞加密,
http://www.reibang.com/p/73d5494af3fb=維吉尼亞密碼加密文件,
http://www.reibang.com/p/03f4d1ec9cae=PL/SQL筆記,
http://www.reibang.com/p/644b42f87896=鍵盤黨的網(wǎng)頁瀏覽--Vimium,
http://www.reibang.com/p/76107670e975=面向對象練習三,
http://www.reibang.com/p/4989b9107a2e=Windows下使用藍燈(lantern)后部分軟件無法上網(wǎng)問題,
http://www.reibang.com/p/f70c6424dab3=泛型}