Jsoup中select方法详解
来源:互联网 发布:网络与新媒体专科 编辑:程序博客网 时间:2024/05/17 23:31
采用CSS或类似jquery 选择器(selector)语法来处理HTML文档中的数据。
利用方法:Element.select(String selector)和Elements.select(String selector)。
- File input = new File("/tmp/input.html");
- Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
- Elements links = doc.select("a[href]"); // a with href
- Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png
- Element masthead = doc.select("div.masthead").first(); // div with class=masthead
- Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
- Document doc = Jsoup.parse(pageString);
- Document doc = Jsoup.connect(url).get();
描述
Jsoup的元素支持类似CSS或(jquery)的选择器语法的查找匹配的元素,可实现功能强大且鲁棒性好的查询。
jsoup elements support a CSS(or jquery) like selector syntax to find matching elements, that allows very powerful and robust queries.
Select方法可作用于Document、Element或Elements,且是上下文相关的,因此可实现指定元素的过滤,或者链式选择访问。
The selectmethod is available in a Document, Element, or in Elements. It is contextual, so you can filter by selecting from a specific element, or by chaining select calls.
选择(操作)返回元素列表(Elements),并提供一组方法来提取或处理结果。
Select returns a list of Elements (as Elements), which provides a range of methods to extract and manipulate the results.
选择器概要(Selector overview)
tagname: find elements by tag, e.g. a
ns|tag: find elements by tag in a namespace, e.g. fb|name finds <fb:name> elements
#id: find elements by ID, e.g. #logo
.class: find elements by class name, e.g. .masthead
[attribute]: elements with attribute, e.g. [href]
[^attr]: elements with an attribute name prefix, e.g. [^data-] finds elements with HTML5 dataset attributes
[attr=value]: elements with attribute value, e.g. [width=500]
[attr^=value], [attr$=value], [attr*=value]: elements with attributes that start with, end with, or contain the value, e.g. [href*=/path/]
[attr=~regex]: elements that have the attribute key, that its value matches the supplied regular expression; e.g. img[src~=(?i)\.(png|jpe?g)]
*: all elements, e.g. *
ns|tag:通过标签在命名空间查找元素,例如:fb|name查找<fb:name>元素
#id:通过ID查找元素,例如#logo
.class:通过类型名称查找元素,例如.masthead
[attribute]:带有属性的元素,例如[href]
[^attr]:带有名称前缀的元素,例如[^data-]查找HTML5带有数据集(dataset)属性的元素
[attr=value]:带有属性值的元素,例如[width=500]
[attr^=value],[attr$=value],[attr*=value]:包含属性且其值以value开头、结尾或包含value的元素,例如[href*=/path/]
[attr~=regex]:属性值满足正则表达式的元素,例如img[src~=(?i)\.(png|jpe?g)]
*:所有元素,例如*
el#id: elements with ID, e.g. div#logo
el.class: elements with class, e.g. div.masthead
el[attr]: elements with attribute, e.g. a[href]
Any combination, e.g. a[href].highlight
ancestor child: child elements that descend from ancestor, e.g. .body p finds p elements anywhere under a block with class "body"
parent > child: child elements that descend directly from parent, e.g. div.content > p finds p elements; and body > * finds the direct children of the body tag
siblingA + siblingB: finds sibling B element immediately preceded by sibling A, e.g. div.head + div
siblingA ~ siblingX: finds sibling X element preceded by sibling A, e.g. h1 ~ p
el, el, el: group multiple selectors, find unique elements that match any of the selectors; e.g. div.masthead, div.logo
el#id::带有ID的元素ID,例如div#logo
el.class:带类型的元素,例如. div.mastheadel[attr]:包含属性的元素,例如a[href]
任意组合:例如a[href].highlight
ancestor child:继承自某祖(父)元素的子元素,例如.body p查找“body”块下的p元素
parent > child:直接为父元素后代的子元素,例如: div.content > pf查找p元素,body > * 查找body元素的直系子元素
siblingA + siblingB:查找由同级元素A前导的同级元素,例如div.head + div
siblingA ~ siblingX:查找同级元素A前导的同级元素X例如h1 ~ p
el, el, el:多个选择器组合,查找匹配任一选择器的唯一元素,例如div.masthead, div.logo
el:lt(n): find elements whose sibling index (i.e. its position in the DOM tree relative to its parent) is less than n; e.g. td:lt(3)
el:gt(n): find elements whose sibling index is greater than n; e.g. div p:gt(2)
el:eq(n): find elements whose sibling index is equal to n; e.g. form input:eq(1)
el:has(seletor): find elements that contain elements matching the selector; e.g. div:has(p)
el:contains(text): find elements that contain the given text. The search is case-insensitive; e.g. p:contains(jsoup)
el:matches(regex): find elements whose text matches the specified regular expression; e.g. div:matches((?i)login).
Note that all of the above indexed pseudo-selectors are 0-based, that is, the first element is at index 0, the second at 1, etc.
:lt(n):查找索引值(即DOM树中相对于其父元素的位置)小于n的同级元素,例如td:lt(3)
:gt(n):查找查找索引值大于n的同级元素,例如div p:gt(2):eq(n) :查找索引值等于n的同级元素,例如form input:eq(1)
:has(seletor):查找匹配选择器包含元素的元素,例如div:has(p)
:not(selector):查找不匹配选择器的元素,例如div:not(.logo)
:contains(text):查找包含给定文本的元素,大小写铭感,例如p:contains(jsoup)
:containsOwn(text):查找直接包含给定文本的元素
:matches(regex):查找其文本匹配指定的正则表达式的元素,例如div:matches((?i)login)
:matchesOwn(regex):查找其自身文本匹配指定的正则表达式的元素
注意:上述伪选择器是0-基数的,亦即第一个元素索引值为0,第二个元素index为1等
详见SelectorAPI 参考资料所列全部信息和细节。
【原文】http://jsoup.org/cookbook/extracting-data/selector-syntax
相关资料:http://www.ibm.com/developerworks/cn/java/j-lo-jsouphtml/
转载自: http://blog.csdn.net/hwwn2009/article/details/41748851
- Jsoup中select方法详解
- Jsoup中select方法详解
- jsoup中select用法详解
- Jsoup文档方法详解
- Jsoup解析HTML方法详解文档
- JSOUP 数据分析中select的用法介绍
- Jsoup详解
- jsoup详解
- jsoup select 选择器
- jsoup select 选择器
- jsoup select 选择器
- jsoup select 选择器
- jsoup select 选择器
- jsoup select 选择器
- jsoup select 选择器
- jsoup select 选择器
- Jsoup select 选择器
- Jsoup的select选择器
- Storm高级原语(四) — Trident API 综述
- oracle设置定时任务来调用存储过程完成统计(每小时计时一次存到hour表)
- LA 3303 / POJ 2675 Songs (数学证明+排序)
- C++读取并逐行显示txt内容
- Android 截图 代码 截屏 保存图片到本地 根据本地图片路径 显示图片
- Jsoup中select方法详解
- 开发和常用工具推荐清单
- IOS面试问题总结
- git flow
- Storm高级原语(四) — Trident API 综述
- DOJO(一) Hello Dojo!!
- Single Number -- leetcode
- 重写view实现viewpager下导航圆点
- Struts2拷贝jar包容易犯的错