`

jsoup 的选择器一览表[转]

阅读更多
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 


下面是 jsoup 所支持的选择器列表:
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. *

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 > 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

Pseudo selectors

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.

资源来自:http://www.oschina.net/bbs/thread/10224
分享到:
评论

相关推荐

    jsoup 选择器(一)常规选择器

    NULL 博文链接:https://injavawetrust.iteye.com/blog/2279338

    jsoup 网页信息 抓取 选择器

    此抓取是抓取新浪高尔夫频道的新闻,采用的是jsoup 选择器抓取,比起httpparse 更好,更高效的抓取。

    JSoup CSS选择器用法大全

    备查,搞爬虫开发者必备

    Jsoup select选择器的简单使用案例

    Jsoup select选择器基本的使用方法,一般套路都是打开浏览器开发者工具,找到需要爬取的网页信息所在的网页标签,标记。然后用选择器把子标签所在的“路径”用语句拼接起来。。select(".slst.mtw&gt;ul&gt;li&gt;h3&gt;a");例如要...

    Jsoup

    Jsoup

    jsoup-1.15.3.jar

    jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jar,jsoup-1.15.3.jarjsoup-...

    Jsoup 1.5.2 和jsoup 1.6

    Jsoup 1.5.2 和jsoup 1.6 开发包jar包,开发文档,源码包

    jsoup 1.7.1API chm

    jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。...2. 使用DOM或CSS选择器来查找、取出数据; 3. 可操作HTML元素、属性、文本; jsoup是基于MIT协议发布的,可放心使用于商业项目。

    JsoupAPI(jsoup帮助文档)

    JsoupAPI jsoup最新版帮助文档(1.10.2)

    jsoup-1.14.3-API文档-中文版.zip

    赠送jar包:jsoup-1.14.3.jar; 赠送原API文档:jsoup-1.14.3-javadoc.jar; 赠送源代码:jsoup-1.14.3-sources.jar; 赠送Maven依赖信息文件:jsoup-1.14.3.pom; 包含翻译后的API文档:jsoup-1.14.3-javadoc-API...

    jsoup中文帮助文档

    7. 使用选择器语法来查找元素 8. 从元素集合抽取属性、文本和html内容 9. URL处理 10. 程序示例:获取所有链接数据修改 11. 设置属性值 12. 设置元素的html内容 13. 设置元素的文本内容html清理 14. 消除不受信任的...

    jsoup-1.6.1.jar

    jsoup 是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。... 使用DOM或CSS选择器来查找、取出数据; 3. 可操作HTML元素、属性、文本; jsoup是基于MIT协议发布的,可放心使用于商业项目。

    JsoupXpath

    整理JsoupXpath( ... 为了在java里也享受xpath的强大与方便但又苦于找不到一款足够强大的xpath解析器,故开发了JsoupXpath。JsoupXpath的实现逻辑清晰,扩展方便, 支持几乎全部常用的xpath语法。

    Jsoup库文件;Jsoup解析Java包

    Jsoup库文件;Jsoup解析Java包

    jsoup:Java HTML 解析器

    使用 DOM 遍历或 CSS 选择器查找并提取数据 操作HTML 元素、属性和文本 根据安全列表清理用户提交的内容,以防止 XSS 攻击 输出整洁的 HTML jsoup 旨在处理各种常见的 HTML;从原始和验证,到无效的标签汤;jsoup 将...

    jsoup 帮助文档1.6.3

    jsoup 是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操作方法来取出和操作数据。 maven依赖: &lt;!-- jsoup ...

    ksoup,jsoup的kotlin包装器.zip

    使用pojo和委托属性的jsoup的kotlin包装器

    java 实现word 转PDF (采用第三方技术 IText、Poi、Jsoup)

    java 实现word 转PDF (采用第三方技术 IText、Poi、Jsoup)iText-2.0.8.jar core-renderer.jar iTextAsian.jar iTextAsianCmaps.jar jsoup-1.8.1.jar

    jsoup包和源码

    jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的...2. 使用DOM或CSS选择器来查找、取出数据; 3. 可操作HTML元素、属性、文本;

    jsoup1.10.3包和jsoupApi帮助文档

    最新jsoup1.10.3和jsoupApi帮助文档

Global site tag (gtag.js) - Google Analytics