[英]Iterate <div> inside <ul> tag Java - Jsoup
I'm trying to get all <div>
inside a <ul>
tag using jsoup.我正在尝试使用 jsoup 将所有
<div>
放入<ul>
标记中。
This is the HTML这是 HTML
<html>
<head>
<title>Try jsoup</title>
</head>
<body>
<ul class="product__listing product__grid">
<div class="product-item">
<div class="content-thumb_gridpage">
<a class="thumb" href="index1.html" title="Tittle 1">
</div>
</div>
<div class="product-item">
<div class="content-thumb_gridpage">
<a class="thumb" href="index2.html" title="Tittle 2">
</div>
</div>
<div class="product-item">
<div class="content-thumb_gridpage">
<a class="thumb" href="index3.html" title="Tittle 3">
</div>
</div>
</ul>
</body>
</html>
What I'm trying to iterate is all <div class="product-item">
so then I can add to a list all <a class="thumb">
properties我要迭代的是所有
<div class="product-item">
所以我可以将所有<a class="thumb">
属性添加到列表中
List-product-details
[0] href="index1.html" title="Tittle 1"
[1] href="index2.html" title="Tittle 2"
[2] href="index3.html" title="Tittle 3"
Note that there can be 'N' product-item
div请注意,可以有“N”
product-item
div
Here is What I got so far:这是我到目前为止得到的:
Elements productList = sneakerList.select("ul.product__listing product__grid");
Elements product = productList.select("ul.product-item");
for (int i = 0; i < product.size(); i++) {
Elements productInfo = product.get(i).select("div.product-item").select("div.content-thumb_gridpage").select("a.thumb");
System.out.format("%s %s %s\n", productInfo.attr("title"), productInfo.attr("href"), productInfo.text());
}
Did you try debugging line by line and checking at which line your code doesn't do what you expect?您是否尝试过逐行调试并检查您的代码在哪一行没有达到您的预期? I see two mistakes.
我看到两个错误。
"ul.product__listing product__grid"
contains a space."ul.product__listing product__grid"
包含一个空格。 Now it means: find element ul
with class product__listing
and inside search for element <product__grid> </product__grid>
.product__listing
查找元素ul
并在内部搜索元素<product__grid> </product__grid>
。 You probably meant: select element ul
having class product__listing
and having class product__grid
.ul
具有 class product__listing
并具有 class product__grid
。 You have to use dot .
.
before second class name and remove space to look at the same level."ul.product__listing.product__grid"
."ul.product__listing.product__grid"
。"ul.product-item"
."ul.product-item"
。 It will return empty result.ul
and you're searching for another ul
.ul
里面并且你正在寻找另一个ul
。 Selector should be relative to where you are so using only ".product-item"
will be enough.".product-item"
就足够了。 And now I get the ouput:现在我得到了输出:
Tittle 1 index1.html
Tittle 2 index2.html
Tittle 3 index3.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.