jsoup|jsoup中选择器select()的使用(jquery,css选择器风格)

没用过Jquery和css的选择器,所以看了Jsoup的介绍后还是不会用select选择器,然后去看了下Jquery选择器的使用,才发觉jsoup真心强大,所以做个简单记录。
选择器简介:
选择器既可以作为元素选择器也可以作为属性选择器,允许您通过标签名、属性名或内容对 HTML 元素进行选择。
选择器允许您对 HTML 元素组或单个元素进行操作。
在 HTML DOM 术语中:选择器允许您对 DOM 元素组或单个 DOM 节点进行操作。
元素选择器: select("p") 选取 元素。
【jsoup|jsoup中选择器select()的使用(jquery,css选择器风格)】 select("p.intro") 选取所有 class="intro" 的 元素。
select("p#demo") 选取 id="demo" 的第一个 元素。

属性选择器: select("[href]") 选取所有带有 href 属性的元素。
select("[href='https://www.it610.com/article/#']") 选取所有带有 href 值等于 "#" 的元素。
select("[href!='#']") 选取所有带有 href 值不等于 "#" 的元素。
select("[href$='.jpg']") 选取所有 href 值以 ".jpg" 结尾的元素。

更多的选择器实例:

语法 描述
$(this) 当前 HTML 元素
$("p") 所有 元素
$("p.intro") 所有 class="intro" 的 元素
$(".intro") 所有 class="intro" 的元素
$("#intro") id="intro" 的第一个元素
$("ul li:first") 每个
    的第一个
  • 元素
$("[href$='.jpg']") 所有带有以 ".jpg" 结尾的属性值的 href 属性
$("div#intro .head") id="intro" 的元素中的所有 class="head" 的元素



更多的使用方法可以参考Jquery选择器参考手册:http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp 参考:http://www.w3school.com.cn/jquery/jquery_selectors.asp http://jsoup.org/cookbook/extracting-data/selector-syntax



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. a
  • ns|tag: find elements by tag in a namespace, e.g. fb|name finds 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#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 > pfinds 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
Pseudo selectors
  • :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)
  • :gt(n): find elements whose sibling index is greater than n; e.g. div p:gt(2)
  • :eq(n): find elements whose sibling index is equal to n; 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.


    推荐阅读