没用过Jquery和css的选择器,所以看了Jsoup的介绍后还是不会用select选择器,然后去看了下Jquery选择器的使用,才发觉jsoup真心强大,所以做个简单记录。
选择器简介:
选择器既可以作为元素选择器也可以作为属性选择器,允许您通过标签名、属性名或内容对 HTML 元素进行选择。
选择器允许您对 HTML 元素组或单个元素进行操作。
在 HTML DOM 术语中:选择器允许您对 DOM 元素组或单个 DOM 节点进行操作。
元素选择器:
select("p") 选取 <p> 元素。
select("p.intro") 选取所有 class="intro" 的 <p> 元素。
select("p#demo") 选取 id="demo" 的第一个 <p> 元素。
属性选择器:
select("[href]") 选取所有带有 href 属性的元素。
select("[href='#']") 选取所有带有 href 值等于 "#" 的元素。
select("[href!='#']") 选取所有带有 href 值不等于 "#" 的元素。
select("[href$='.jpg']") 选取所有 href 值以 ".jpg" 结尾的元素。
更多的选择器实例:
| 语法 | 描述 |
|---|---|
| $(this) | 当前 HTML 元素 |
| $("p") | 所有 <p> 元素 |
| $("p.intro") | 所有 class="intro" 的 <p> 元素 |
| $(".intro") | 所有 class="intro" 的元素 |
| $("#intro") | id="intro" 的第一个元素 |
| $("ul li:first") | 每个 <ul> 的第一个 <li> 元素 |
| $("[href$='.jpg']") | 所有带有以 ".jpg" 结尾的属性值的 href 属性 |
| $("div#intro .head") | id="intro" 的 <div> 元素中的所有 class="head" 的元素 |
更多的使用方法可以参考Jquery选择器参考手册:http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp
Description
jsoup elements support a CSS (or jquery) like selector syntax to find matching elements, that allows very powerful and robust queries.
The select method 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.
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.ans|tag: find elements by tag in a namespace, e.g.fb|namefinds<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 with attribute values that match the regular expression; e.g.img[src~=(?i)\.(png|jpe?g)]*: all elements, e.g.*
Selector combinations
el#id: elements with ID, e.g.div#logoel.class: elements with class, e.g.div.mastheadel[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 pfindspelements anywhere under a block with class "body"parent > child: child elements that descend directly from parent, e.g.div.content > pfindspelements; andbody > *finds the direct children of the body tagsiblingA + siblingB: finds sibling B element immediately preceded by sibling A, e.g.div.head + divsiblingA ~ siblingX: finds sibling X element preceded by sibling A, e.g.h1 ~ pel, el, el: group multiple selectors, find unique elements that match any of the selectors; e.g.div.masthead, div.logo
Pseudo selectors
:lt(n): find elements whose sibling index (i.e. its position in the DOM tree relative to its parent) is less thann; e.g.td:lt(3):gt(n): find elements whose sibling index is greater thann; e.g.div p:gt(2):eq(n): find elements whose sibling index is equal ton; e.g.form input:eq(1):has(seletor): find elements that contain elements matching the selector; e.g.div:has(p):not(selector): find elements that do not match the selector; e.g.div:not(.logo):contains(text): find elements that contain the given text. The search is case-insensitive; e.g.p:contains(jsoup):containsOwn(text): find elements that directly contain the given text:matches(regex): find elements whose text matches the specified regular expression; e.g.div:matches((?i)login):matchesOwn(regex): find elements whose own text matches the specified regular expression- Note that the above indexed pseudo-selectors are 0-based, that is, the first element is at index 0, the second at 1, etc
See the Selector API reference for the full supported list and details.

本文介绍了Jsoup选择器的强大功能,包括元素选择器和属性选择器,并提供了多种选择器实例,便于读者理解和应用。
&spm=1001.2101.3001.5002&articleId=8018239&d=1&t=3&u=9cfc283efb1342579dbe4a14d95dbb18)
982

被折叠的 条评论
为什么被折叠?



