Jsoup(三)-- Jsoup使用选择器语法查找DOM元素
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(三)-- Jsoup使用选择器语法查找DOM元素的更多相关文章
- (三)Jsoup 使用选择器语法查找 DOM 元素
第一节: Jsoup 使用选择器语法查找 DOM 元素 Jsoup使用选择器语法查找DOM元素 我们前面通过标签名,Id,Class样式等来搜索DOM,这些是不能满足实际开发需求的, 很多时候我们需要 ...
- Jsoup(二)-- Jsoup查找DOM元素
一.Jsoup查找DOM元素的方法 getElementById(String id) 根据id 来查询DOM getElementsByTag(String tagName) 根据tag 名称来查询 ...
- (二)Jsoup 查找 DOM 元素
第一节: Jsoup 查找 DOM 元素 getElementById(String id) 根据 id 来查询 DOM getElementsByTag(String tagName) 根据 tag ...
- Jsoup查找dom元素
package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...
- JavaScript的基础语法及DOM元素和事件
一,JavaScript是什么? 1,JavaScript简称:js,是一种浏览器解释型语言,嵌套在HTML文件中交给浏览器解释执行.主要用来实现网页的动态效果,用户交互及前后端的数据传输等. 2,J ...
- jQuery常用的查找Dom元素方法
废话不多说,先来个总结,然后下面是demo 一. 同级节点之间的检索(检索深度N=0) next()是在兄弟节点中,往后匹配; prev()是在兄弟节点中,往前匹配. 二. 父级/子级节点的检索(检索 ...
- crawler_jsoup HTML解析器_使用选择器语法来查找元素
参照:http://www.open-open.com/jsoup/selector-syntax.htm 使用选择器语法来查找元素 问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. ...
- jQuery 数据 DOM 元素 核心 属性
jQuery 参考手册 - 数据 .clearQueue() 从序列中删除仍未运行的所有项目 .clearQueue(queueName) $("div").clearQueue( ...
- JS选取DOM元素的方法
摘自JavaScript权威指南(jQuery根据样式选择器查找元素的终极方式是 先用getElementsByTagName(*)获取所有DOM元素,然后根据样式选择器对所有DOM元素进行筛选) 今 ...
随机推荐
- add new row to data.frame/dataframe
df<-NULL new_row<-data.frame(colA="xxx",colB=123) df<-rbind(df,new_row)
- Entity Framework Code First - Change Tracking
In this post we will be discussing about change tracking feature of Entity Framework Code First. Cha ...
- hashMap 根据已有知识知道的
public V put(K key, V value) { //假如table为空 if (table == EMPTY_TABLE) { inflateTable(threshold); } // ...
- Python之生成二面体群元素
from sympy.combinatorics.named_groups import DihedralGroup from collections import Counter n = 12 G ...
- Spring JDBC SqlQuery类示例
org.springframework.jdbc.object.SqlQuery类提供了表示SQL查询的可重用操作对象. 使用到的 Student 表的结构如下 - CREATE TABLE Stud ...
- Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.【转】
今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...
- Python 中 __all__ 的作用
1.测试文件foo.py # -*- coding: utf-8 -*- # import sys # reload(sys) # sys.setdefaultencoding('gbk') __al ...
- HDU 3455 Leap Frog(线性DP)
Problem Description Jack and Jill play a game called "Leap Frog" in which they alternate t ...
- CentOS 7系统查看系统版本和机器位数
前言 由于不经常使用linux,每当使用的时候就是安装软件,安装软件的时候就要选择安装包平台,是32位的还是64位的.这时候突然发现不知道怎么查,于是百度.虽然轻而易举百度出来,但仍旧没有自己的笔记看 ...
- springBoot配置文件application.properties
# =================================================================== # COMMON SPRING BOOT PROPERTIE ...