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
有多个父母
标签如上。我希望我的循环适用于所有人
父标签。我试过,Elements indProducts = html.select(“li”);这甚至拉动了孩子李。我不想那样。我想编写这样的代码,如果
课程从专业图片开始,然后将被认为是跳过。我该怎么办?是否有一个类似于“喜欢”或类似的选择子句。
2 个解决方案
#1
0
Yes, jSoup provides something similar to like. Check out this selector usage link.
是的,jSoup提供类似的东西。查看此选择器使用链接。
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
输出= 2
Note: Your class
starting with pro pic
is too generic and will return the outermost parent (and also one inner child).
注意:以pro pic开头的课程过于通用,将返回最外层的父级(以及一个内部子级)。
#2
0
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. I don't know if works for Elements.
* PS:我测试了你的内容作为元素。我不知道是否适用于Elements。
#1
0
Yes, jSoup provides something similar to like. Check out this selector usage link.
是的,jSoup提供类似的东西。查看此选择器使用链接。
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
输出= 2
Note: Your class
starting with pro pic
is too generic and will return the outermost parent (and also one inner child).
注意:以pro pic开头的课程过于通用,将返回最外层的父级(以及一个内部子级)。
#2
0
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. I don't know if works for Elements.
* PS:我测试了你的内容作为元素。我不知道是否适用于Elements。