抓取的是我博客的文章标题和链接,使用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存储获取到的分页数据,link存储UI中的数据
Elements title = null;
Elements link = new Elements();
//存储获取到的数据,<链接作为KEY,text作为值>
Map<String,String> map = new HashMap<>();
for(int i =0;;i++){
Document doc = Jsoup.connect("http://www.jianshu.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;
}
//吧元素集合里面的数据存储到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.jianshu.com/p/4502b2802cf8=ArrayList练习-去掉重复元素,
http://www.jianshu.com/p/5565e5c59afd=单表替换密码,
http://www.jianshu.com/p/f23ec9da6ecf=java集合,
http://www.jianshu.com/p/655c2ea168b0=面向对象练习二,
http://www.jianshu.com/p/0eb7a2030b6d=java集合-Set,
http://www.jianshu.com/p/56c9bcc713a2=Killer网卡未正确映射ISP问题解决办法,
http://www.jianshu.com/p/9e5d38c36373=蒙塔卡罗法计算π,
http://www.jianshu.com/p/58b92b4a428a=维吉尼亚加密遇到的问题,
http://www.jianshu.com/p/42b914293435=重装Win7后无法获取更新解决办法,
http://www.jianshu.com/p/6c65417f1237=我的键盘,
http://www.jianshu.com/p/ad52b1207a9a=国庆写代码,
http://www.jianshu.com/p/d9141fccc345=验证哥德巴赫猜想,
http://www.jianshu.com/p/b3d9c603d2c8=Comparator练习-String按照长度比较,
http://www.jianshu.com/p/aba38279d14f=Oracle数据库学习整理,
http://www.jianshu.com/p/d0505d11c819=巧用windows任务栏,
http://www.jianshu.com/p/5af00815b4e4=vim基本操作命令,
http://www.jianshu.com/p/83d2f2f8fff5=维吉尼亚加密,
http://www.jianshu.com/p/73d5494af3fb=维吉尼亚密码加密文件,
http://www.jianshu.com/p/03f4d1ec9cae=PL/SQL笔记,
http://www.jianshu.com/p/644b42f87896=键盘党的网页浏览--Vimium,
http://www.jianshu.com/p/76107670e975=面向对象练习三,
http://www.jianshu.com/p/4989b9107a2e=Windows下使用蓝灯(lantern)后部分软件无法上网问题,
http://www.jianshu.com/p/f70c6424dab3=泛型}