[英]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 有多个父母
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.