首页 技术 正文
技术 2022年11月15日
0 收藏 931 点赞 2,896 浏览 1437 个字

1.Jsoup可以使用类似于CSS或jQuery的语法来查找和操作元素.

2.实例如下:

    public static void main(String[] args) throws Exception{
// 创建httpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httpGet实例
HttpGet httpGet = new HttpGet("http://www.cnblogs.com");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
CloseableHttpResponse response = httpClient.execute(httpGet);
String content = null;
if(response != null){
HttpEntity entity = response.getEntity();
content = EntityUtils.toString(entity, "UTF-8"); // 获取网页内容
Document document = Jsoup.parse(content); // 解析网页,得到文档对象 // 1.查找所有帖子DOM
Elements elements = document.select(".post_item .post_item_body h3 a");
for(Element ele : elements){
System.out.println("博客标题:" + ele.text());
}
System.out.println("------------------------分割线------------------------"); // 2.查找带有href属性的a元素
Elements hrefElements = document.select("a[href]");
for(Element ele : hrefElements){
System.out.println(ele.toString());
}
System.out.println("------------------------分割线------------------------"); // 3.查找扩展名为.png的图片DOM节点
Elements imgElements = document.select("img[src$=.png]");
for(Element ele : imgElements){
System.out.println(ele.toString());
}
System.out.println("------------------------分割线------------------------"); // 4.获取tag为title的第一个DOM元素
Element titleEle = document.getElementsByTag("title").first();
System.out.println("标题为:" + titleEle.text());
}
if(response != null){
response.close();
}
if(httpClient != null){
httpClient.close();
}
}

3.Jsoup学习地址

  开源博客系统-Jsoup

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289