简体   繁体   English

迭代<div>里面<ul>标签 Java - Jsoup</ul></div><div id="text_translate"><p> 我正在尝试使用 jsoup 将所有&lt;div&gt;放入&lt;ul&gt;标记中。</p><p> 这是 HTML</p><pre> &lt;html&gt; &lt;head&gt; &lt;title&gt;Try jsoup&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;ul class="product__listing product__grid"&gt; &lt;div class="product-item"&gt; &lt;div class="content-thumb_gridpage"&gt; &lt;a class="thumb" href="index1.html" title="Tittle 1"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="product-item"&gt; &lt;div class="content-thumb_gridpage"&gt; &lt;a class="thumb" href="index2.html" title="Tittle 2"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="product-item"&gt; &lt;div class="content-thumb_gridpage"&gt; &lt;a class="thumb" href="index3.html" title="Tittle 3"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 我要迭代的是所有&lt;div class="product-item"&gt;所以我可以将所有&lt;a class="thumb"&gt;属性添加到列表中</p><pre>List-product-details [0] href="index1.html" title="Tittle 1" [1] href="index2.html" title="Tittle 2" [2] href="index3.html" title="Tittle 3"</pre><p> 请注意,可以有“N” product-item div</p><p> 这是我到目前为止得到的:</p><pre> Elements productList = sneakerList.select("ul.product__listing product__grid"); Elements product = productList.select("ul.product-item"); for (int i = 0; i &lt; 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()); }</pre></div>

[英]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.我看到两个错误。

  1. The first selector "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> .现在它的意思是:使用 class product__listing查找元素ul并在内部搜索元素<product__grid> </product__grid> You probably meant: select element ul having class product__listing and having class product__grid .您可能的意思是: select 元素ul具有 class product__listing并具有 class product__grid You have to use dot .你必须使用 dot . before second class name and remove space to look at the same level.在第二个 class 名称之前并删除空格以查看同一级别。 So correct selector will be: "ul.product__listing.product__grid" .所以正确的选择器将是: "ul.product__listing.product__grid"
  2. Second selector you're using is "ul.product-item" .您使用的第二个选择器是"ul.product-item" It will return empty result.它将返回空结果。 That's because you're already inside 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM