简体   繁体   中英

jsoup get values based on div tag id

I have an html which has tags as follows.

 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"); 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. What should I do? Is there a select clause that works similar to "like" or something like that.

  • This is more precise, just selects tags li for which the class attributes starts with 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. I don't know if works for Elements.

    Yes, jSoup provides something similar to like. 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).

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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