简体   繁体   English

如何通过Jsoup获取“select”html元素?

[英]How to get a “select” html element by Jsoup?

<section class="my grid">

How can I use Jsoup to optain this element (and all sub elements)? 如何使用Jsoup来获取此元素(以及所有子元素)? The following does not work (is empty): 以下不起作用(为空):

Elements ul = doc.getElementsByClass("my grid");
Elements listGrids=new Elements
for(Element section:doc.getElementsByTag("section"))
{
        if(section.absUrl("Class).equals("my grid")
          listGrids.add(section);
}

I don't know why your current code doesn't work, but it could be because you have a space in your value 我不知道为什么你当前的代码不起作用,但可能是因为你的价值空间

Niko 尼科

This answer is just for your information. 这个答案仅供您参考。 This could be done even easier. 这可以更容易地完成。 Just like 就像

Elements ul = doc.select("section.my.grid");

or for iteration as 或者作为迭代

for(Element section : doc.select("section.my.grid")){
    System.out.println(section.text());
}

Explanation 说明

Esentially you could filter tag based on class by . 实际上你可以根据class by过滤标签 (DOT) selector. (DOT)选择器。 Refer here 请参考这里

For eg - el.class gives all elements with class, eg div.masthead selects all div tags with class masthead. 例如 - el.class为所有元素提供类,例如div.masthead选择所有带有类标头的div标签。 So in your case you have two classes "my" and "grid" for section tag. 所以在你的情况下,你有两个类“my”和“grid”用于section标签。 So just filter like 所以就像过滤器一样

Elements ul = doc.select("section.my");

or

Elements ul = doc.select("section.grid")

This will give you all section tags with a class attribute of my or grid. 这将为您提供具有my或grid的class属性的所有section标签。 But in case if you have multiple combination of "my" class and you want just "my" and "grid" together do nesting. 但是,如果你有多个“我的”类的组合,你只想“我的”和“网格”一起做嵌套。

Elements ul = doc.select("section.my.grid");

Classes are separated by a space. 类由空格分隔。 In your case you add 2 classes to your section ("my" and "grid"). 在您的情况下,您将为您的部分添加2个类(“我的”和“网格”)。

If you want to have a readable class, use a "-" to separate them. 如果您想要一个可读的类,请使用“ - ”分隔它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM