jsoup select 选择器(Day_02)
“自己”这个东西是看不见的,撞上一些别的什么,反弹回来,才会了解“自己”。
所以,跟很强的东西、可怕的东西、水准很高的东西相碰撞,然后才知道“自己”是什么,这才是自我 。
运行环境
JDK8 + IntelliJ IDEA 2018.3
利用方法
Element.select(String selector)和Elements.select(String selector)。
jar包文件
https://files.cnblogs.com/files/papercy/jsoup_jar%E5%8C%85.rar
描述
Jsoup的元素支持类似CSS或(jquery)的选择器语法的查找匹配的元素,可实现功能强大且鲁棒性好的查询。
Select方法可作用于Document、Elements或Element,且是上下文相关的,因此可实现指定元素的过滤,或者链式选择访问。
以下共介绍使用十种方法获取 xml元素
XML代码
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="1">
<name>feifeiye</name>
<age>19</age>
<sex>nv</sex>
</student>
<student id="2" class="two">
<name>papercy</name>
<age id="3" text-color="red" >19</age>
<sex>nan</sex>
</student>
</students>
JAVA代码实现
public static void main(String[] args) {
/*获取document文档,反射path*/
String path=JsoupDemo1.class.getClassLoader().getResource("Students.xml").getPath();
try {
Document document = Jsoup.parse(new File(path),"utf-8");
/*获取elements/element*/
Elements element=document.getAllElements();
System.out.println("element:"+element);
} catch (IOException e) {
e.printStackTrace();
}
}
选择器使用
- 使用 * 选择器
Elements elements = document.select("*");
System.out.println("element * 号选择器:"+elements); 使用class 选择器
Elements elements1 = document.select(".two");
System.out.println("element class 选择器:"+elements1);
- 使用ID 选择器
Elements element1 = document.select("#1");
System.out.println("element1 id 选择器:"+element1);
- 使用 Tag 选择器
Elements elements2 = document.select("name");
System.out.println("eleemnts2 tag 选择器:"+elements2);
- 使用 [attribute]: 利用属性
Elements elements3 = document.select("[text-color]");
System.out.println("elements3 [attribute] 选择器:"+elements3);
- 使用 [^attr=value] : 利用属性值
Elements elements4 = document.select("[text-color=red]");
System.out.println("elements4 [^attr=value] 选择器:"+elements4);
- 使用 parent > child : 查找某个父元素下的直接子元素
Elements elements5 = document.select("student>sex");
System.out.println("element5 parent > child 选择器 :"+elements5);
- 使用 ancestor child : (查找某个元素下子元素)
- Elements element2 = document.select("students name"); System.out.println("element2 ancestor child 选择器:"+element2);
- 使用 containsOwn(text):查找直接包含给定文本的元素
Elements element3 = document.select("name:contains(feifeiye)");
System.out.println("element3 containsOwn(text)选择器:"+element3);
- 使用 eq(n): 查找哪些元素的同级索引值与n相等
Elements elements6 = document.select("student age:eq(1)");
System.out.println("elements6 eq(n)选择器:"+elements6);
心得
希望本无所谓有,无所谓无的。这正如地上的路;其实地上本没有路,走的人多了,也便成了路。
很多东西,只有花时间去做了,才能有个成功与否,站着看的永远比走的慢。或许看上去很厉害,很难的东西,你去实践的时候会发现没有想象中的难。
会不会是取决你做了没做,而不是想了没想。
jsoup select 选择器(Day_02)的更多相关文章
- jsoup select 选择器
转载自:http://blog.csdn.net/zhejingyuan/article/details/11801027 方法 利用方法:Element.select(String selector ...
- (三)Jsoup 使用选择器语法查找 DOM 元素
第一节: Jsoup 使用选择器语法查找 DOM 元素 Jsoup使用选择器语法查找DOM元素 我们前面通过标签名,Id,Class样式等来搜索DOM,这些是不能满足实际开发需求的, 很多时候我们需要 ...
- Jsoup进阶选择器
package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...
- org.jsoup.select.Selector
org.jsoup.select.Selector CSS-like element selector, that finds elements matching a query. Selector ...
- elementUI 学习入门之 Select 选择器
Select 选择器 基础用法 <el-select v-model="val1" placeholder="请输入"> <el-option ...
- Jsoup 标签选择器 选择img标签中src的值
package com.enation.newtest; import java.io.BufferedReader; import java.io.File; import java.io.File ...
- element UI中的select选择器的change方法需要传递多个值
如果直接调用change事件,不传任何参数,则可以获取到当前选中的值(因为默认会将event参数传递过去) 场景: 你需要将select选择器 ”选中的当前元素“ 和 ”其他你需要的值“ 一起传递过去 ...
- Select 选择器
Select 选择器 当选项过多时,使用下拉菜单展示并选择内容. 基础用法 适用广泛的基础单选 v-model的值为当前被选中的el-option的 value 属性值 <template> ...
- css子选择器 :frist-child :nth-child(n) :nth-of-type(n) ::select选择器
记录一下前一段时间使用.学习的几种选择器. 1. :frist-child 选择器n 比如<ul><li></li> <li></li> & ...
随机推荐
- Kubernetes部署metrics-server提示健康检测报错500,简单解决方式
为什么写? 最近有项目要用到HPA(Horizontal Pod Autoscaler)依赖了k8s的 metrics 指标才能做出自动缩扩容的动作,我这边用官方GitHub v0.4.2版本启动不起 ...
- 自动化kolla-ansible部署ubuntu20.04+openstack-victoria之物理机配置-01
自动化kolla-ansible部署ubuntu20.04+openstack-victoria之物理机配置-01 欢迎加QQ群:1026880196 进行交流学习 近期我发现网上有人转载或者复制 ...
- RedHat 7.6 安装 Mysql 8.0.17
# 查看是否安装mysql rpm -qa | grep -i mysql # 如果有,需要卸载旧版本Mysql及相关依赖包 rpm -e MySQL-client-*** # 查看开机启动服务列表状 ...
- IDEA中Maven本地仓库与镜像配置
1 Maven Maven是一个用于项目构建与管理的工具,IDEA自带了Maven,在安装目录下的 plugins\maven\lib\maven3 IDEA也可以集成使用非自带的Maven,也就是自 ...
- Ducci Sequence UVA - 1594
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1,a2,···,an ...
- Day13_69_yield()
yield() 方法 - Thread.yield() 该方法是一个静态方法, 作用是给同一个优先级的线程让位,阻塞当前线程,但是阻塞时间不可以指定,是随机的. - Thread.yield() 和 ...
- Pytorch系列:(一)常用基础操作
各种张量初始化 创建特殊类型的tensor a = torch.FloatTensor(2,3) a = torch.DoubleTensor(2,3) ... 设置pytorch中tensor的默认 ...
- 前端进阶(2)使用fetch/axios时, 如何取消http请求
前端进阶(2)使用fetch/axios时, 如何取消http请求 1. 需求 现在前端都是SPA,我们什么时候需要取消HTTP请求呢? 当我们从一个页面跳转到另外一个页面时,如果前一个页面的请求还没 ...
- 【Spring】SpringIoC大致流程
目录 SpringIoC 是什么? 类图 大致过程 源码分析 SpringIoC 是什么? 官方文档的解释是:IoC也称为依赖注入(DI).在此过程中,对象仅通过构造函数参数,工厂方法的参数或在构 ...
- 也谈如何写一个Webserver(-)
关于如何写一个Webserver,很多大咖都发表过类似的文章.趁着这个五一假期,我也来凑个份子. 我写Webserver的原因,还得从如何将http协议传送的消息解析说起.当时,我只是想了解一下htt ...