简体   繁体   English

jsoup根据div标签ID获取值

[英]jsoup get values based on div tag id

I have an html which has tags as follows. 我有一个具有如下标签的html。

 parent   <li class="pro pic notSold" status="0" >
 child      <ul><li></li><ul>
 parent   <li class="pro pic soldOut" status="-1" >
 child      <ul><li></li><ul>

there are multiple parent 有多个父母

  • tags as above. 标签如上。 I want my loop to work for all 我希望我的循环为所有人工作
  • parent tags. 父标签。 I tried, Elements indProducts = html.select("li"); 我尝试过,Elements indProducts = html.select(“ li”); This was pulling even the child li. 这甚至拉孩子李。 I dont want that. 我不想要那个。 I want to code such that if the 我想编码,如果
  • class starts with pro pic, then it would be considered else skip. 类以pro pic开始,然后将其视为跳过。 What should I do? 我该怎么办? Is there a select clause that works similar to "like" or something like that. 是否有一个select子句的工作方式类似于“ like”之类。

  • This is more precise, just selects tags li for which the class attributes starts with pro pic: 这更精确,只需选择标签li,其类别属性以pro pic开头:

    Elements litags = yourcontent.select("li[class^=pro pic]");
    

    Or if you want to be sure that you take only the first level of childrens you can use this: 或者,如果您想确保只参加第一级儿童课程,则可以使用以下方法:

    Elements litags = yourcontent.select(" > li[class^=pro pic]");  
    

    *PS: I tested with yourcontent as Element. * PS:我用您的内容作为元素进行了测试。 I don't know if works for Elements. 我不知道是否适用于Elements。

    Yes, jSoup provides something similar to like. 是的,jSoup提供了类似的东西。 Check out this selector usage link. 查看此选择器用法链接。

    You could try something like this: 您可以尝试这样的事情:

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.select.Elements;
    
    
    public class JSoupTest 
    {
        public static void main(String[] args) 
        {
             String html =   "<li class='pro pic notSold' status='0' >";
             html+=      "<ul><li></li><ul>";
             html+=   "<li class='pro pic soldOut' status='-1' >";
             html+=      "<ul><li></li><ul>";
    
             Document doc = Jsoup.parse(html);
             Elements elems = doc.select("[class^=pro pic]");
    
            System.out.println(elems.size());
        }
    }
    

    Output = 2

    Note: Your class starting with pro pic is too generic and will return the outermost parent (and also one inner child). 注意:您的以pro pic开头的class太通用,并且将返回最外面的父级(以及一个内部子级)。

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

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