zoukankan      html  css  js  c++  java
  • 使用document.select(Jquery Css selector) selector:看jsoup文档来敲案例学习 selector选择器

    1.

    //解析xml的文档对象
    Document document= Jsoup.parse("<?xml version="1.0" encoding="utf-8" ?> " +
    "<students> " +
    " <student id="1" class="tty"> " +
    " <name color="pink">唐婷栎</name> " +
    " <age>19</age> " +
    " <sex>女</sex> " +
    " <favorite>study</favorite> " +
    " </student> " +
    " <student id="2" class="tjx"> " +
    " <name color="blue">唐峻熙</name> " +
    " <age>4</age> " +
    " <sex>男</sex> " +
    " <favorite>play</favorite> " +
    " </student> " +
    " " +
    "</students>");
    Elements elements=document.getAllElements();
    System.out.println("elements:"+elements);

    2.

    //解析html的文档对象
    URL url = null;
    try {
    url = new URL("https://www.jianshu.com/u/b7b2c6ed9284");
    Document document = Jsoup.parse(url,10000);
    System.out.println("document:"+document);
    } catch (IOException e) {
    e.printStackTrace();
    }

    c.parse(URL url,int timeoutMillis):通过网络路径指定的html或xml的文档对象

    Jsoup的强大在于它对文档元素的检索,Select方法将返回一个Elements集合,并提供一组方法来抽取和处理结果,即Jsoup的选择器语法。

    1、Selector选择器基本语法

    tagname: 通过标签查找元素,比如:a

    ns|tag: 通过标签在命名空间查找元素,比如:可以用 fb|name 语法来查找 <fb:name> 元素

    #id: 通过ID查找元素,比如:#logo

    .class: 通过class名称查找元素,比如:.masthead

    [attribute]: 利用属性查找元素,比如:[href]

    [^attr]: 利用属性名前缀来查找元素,比如:可以用[^data-] 来查找带有HTML5 Dataset属性的元素

    [attr=value]: 利用属性值来查找元素,比如:[width=500]

    [attr^=value], [attr$=value], [attr*=value]: 利用匹配属性值开头、结尾或包含属性值来查找元素,比如:[href*=/path/]

    [attr~=regex]: 利用属性值匹配正则表达式来查找元素,比如: img[src~=(?i).(png|jpe?g)]

    *: 这个符号将匹配所有元素

    2、Selector选择器组合使用语法

    el#id: 元素+ID,比如: div#logo

    el.class: 元素+class,比如: div.masthead

    el[attr]: 元素+class,比如: a[href]

    任意组合,比如:a[href].highlight

    ancestor child: 查找某个元素下子元素,比如:可以用.body p 查找在”body”元素下的所有 p元素

    parent > child: 查找某个父元素下的直接子元素,比如:可以用div.content > p 查找 p 元素,也可以用body > * 查找body标签下所有直接子元素

    siblingA + siblingB: 查找在A元素之前第一个同级元素B,比如:div.head + div

    siblingA ~ siblingX: 查找A元素之前的同级X元素,比如:h1 ~ p

    el, el, el:多个选择器组合,查找匹配任一选择器的唯一元素,例如:div.masthead, div.logo

    3、Selector伪选择器语法

    :lt(n): 查找哪些元素的同级索引值(它的位置在DOM树中是相对于它的父节点)小于n,比如:td:lt(3) 表示小于三列的元素

    :gt(n):查找哪些元素的同级索引值大于n,比如: div p:gt(2)表示哪些div中有包含2个以上的p元素

    :eq(n): 查找哪些元素的同级索引值与n相等,比如:form input:eq(1)表示包含一个input标签的Form元素

    :has(seletor): 查找匹配选择器包含元素的元素,比如:div:has(p)表示哪些div包含了p元素

    :not(selector): 查找与选择器不匹配的元素,比如: div:not(.logo) 表示不包含 class="logo" 元素的所有 div 列表

    :contains(text): 查找包含给定文本的元素,搜索不区分大不写,比如: p:contains(jsoup)

    :containsOwn(text): 查找直接包含给定文本的元素

    :matches(regex): 查找哪些元素的文本匹配指定的正则表达式,比如:div:matches((?i)login)

    :matchesOwn(regex): 查找自身包含文本匹配指定正则表达式的元素

    注意:上述伪选择器索引是从0开始的,也就是说第一个元素索引值为0,第二个元素index为1等。

    部分代码展示:

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;

    import java.io.File;
    import java.io.IOException;

    public class jsoupDemo5 {
    public static void main(String[] args) {
    //使用多种select选择器
    String path= jsoupDemo5.class.getClassLoader().getResource("students.xml").getPath();
    try {
    Document document=Jsoup.parse(new File(path),"UTF-8");
    //方法1:使用*选择器
    System.out.println("使用*选择器");
    Elements elements=document.select("*");
    System.out.println("elements:"+elements);

    System.out.println("*****************");

    //方法2:使用id选择器
    System.out.println("使用id选择器");
    Elements element=document.select("#1");
    System.out.println("element:"+element);

    System.out.println("*****************");

    //方法3:使用class选择器
    System.out.println("使用class选择器");
    Elements element1=document.select(".tjx");
    System.out.println("element1:"+element1);

    System.out.println("*****************");

    //方法4:使用tag选择器
    System.out.println("使用tag选择器");
    Elements element2=document.select("name");
    System.out.println("element2:"+element2);

    System.out.println("*****************");

    //方法5:使用[attr=value]选择器
    System.out.println("使用[attr=value]选择器");
    Elements element3=document.select("[width=50]");
    System.out.println("element3:"+element3);

    System.out.println("*****************");

    //方法6:使用eq选择器
    System.out.println("使用eq选择器");
    Elements element4=document.select("student age:eq(1)");
    System.out.println("element4:"+element4);

    System.out.println("*****************");

    //方法7:使用contains选择器
    System.out.println("使用contains选择器");
    Elements element5=document.select("age:contains(19)");
    System.out.println("element5:"+element5);

    System.out.println("*****************");

    //方法8:使用not选择器
    System.out.println("使用not选择器");
    Elements element6=document.select("student:not(.tjx)");
    System.out.println("element6:"+element6);

    System.out.println("*****************");

    //方法9:使用ancestor child选择器
    System.out.println("使用ancestor child选择器");
    Elements element7=document.select("students name");
    System.out.println("element7:"+element7);

    System.out.println("*****************");

    //方法10:使用parent>child选择器
    System.out.println("使用parent>child选择器");
    Elements element8=document.select("student>sex");
    System.out.println("element8:"+element8);


    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
  • 相关阅读:
    delphi 字符串查找替换函数 转
    Delphi流的操作
    【BZOJ1316】树上的询问 点分治+set
    【BZOJ2406】矩阵 二分+有上下界的可行流
    【BZOJ1853/2393】[Scoi2010]幸运数字/Cirno的完美算数教室 DFS+容斥
    【BZOJ4999】This Problem Is Too Simple! 离线+树状数组+LCA
    【BZOJ2427】[HAOI2010]软件安装 Tarjan+树形背包
    【BZOJ3217】ALOEXT 替罪羊树+Trie树
    【BZOJ1336】[Balkan2002]Alien最小圆覆盖 随机增量法
    【BZOJ3435】[Wc2014]紫荆花之恋 替罪点分树+SBT
  • 原文地址:https://www.cnblogs.com/www-tty-com/p/13530845.html
Copyright © 2011-2022 走看看