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
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.