JSOUP get element with multiple classes

后端 未结 1 797
失恋的感觉
失恋的感觉 2021-01-23 16:32

I am trying to read all

s with the class = \"listitem show-in-category\" from an HTML page. Furthermore, I want to get the itemprop
相关标签:
1条回答
  • 2021-01-23 17:13

    The spaces in the class name are actually separator between class names. So the div is part of two classes, namely listitem and show-in-category

    If you must select only elements that match both classes, you can do this:

    Elements mydesiredElems = doc.select("div.listitem.show-in-category")
    

    The dot followed by a class name is the css selector for a class. They can be concatenated and that results in adding this class as a requirement to match the element.

    About your second question: Jsoup can easily get the img element for you. Suppose that myDivEl is one of your divs

    Element imgEl = myDivEl.select("img");
    String altStr = imgEl.attr("alt");
    String titleStr = imgEl.attr("title");
    

    UPDATE after question was edited to point out what the OP wants:

    Element itemPropPEl = myDivEl.select("p[itemprop=description]");
    String descStr = itemPropPEl.text();
    
    0 讨论(0)
提交回复
热议问题