gpt4 book ai didi

java - JSoup 按属性和类搜索

转载 作者:行者123 更新时间:2023-12-02 03:21:33 26 4
gpt4 key购买 nike

你可以这样做:

Elements links = doc.select("a[href]");

查找所有具有 href 属性的“a”元素。

你可以这样做:

doc.getElementsByClass("title")

获取具有名为“title”的类的所有元素

但是我怎样才能同时做到这两点呢? (即搜索带有“href”标签的“a”元素,该标签也具有“title”类)。

最佳答案

你可以简单地拥有

Elements links = doc.select("a[href].title");

这将选择所有 <a>href具有 title 的属性类(class)。类(class)通过by prepending it with a dot :

Selector combinations

  • Any combination, e.g. a[href].highlight

完整示例:

public static void main(String[] args) {
Document doc = Jsoup.parse(""
+ "<div>"
+ " <a href='link1' class='title another'>Link 1</a>"
+ " <a href='link2' class='another'>Link 2</a>"
+ " <a href='link3'>Link 3</a>"
+ "</div>");

Elements links = doc.select("a[href].title");
System.out.println(links); // prints "<a href="link1" class="title another">Link 1</a>"
}

关于java - JSoup 按属性和类搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39551832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com