繁体   English   中英

使用jsoup选择div标签

[英]selecting a div tag with jsoup

嗨,我有2个div既显示日期又具有相同的名称。 我现在要专门显示第一个div,以后再显示第二个日期。 我尝试搜索,但是发现可以指定div name元素并通过它区分两个div。

(This is the first date which I want to display)
<div class="home-weather-sub-div-bar">
<span class="datetext1">Sunday, March  3, 2013 updated 14:45:00 CET</span>
</div>

(This is the second date which I want to display)    
<div class="home-weather-sub-div-bar">
<span class="datetext1">Monday, March  4, 2013 until noon</span>
</div>

为了清楚起见,我尝试选择“ div.home-weather-sub-div-bar”和“ span.datetext1”,但仍会显示2013年3月3日(星期日),CET星期一更新14:45:00,然后它也将在2013年3月4日连接到同一行中午。

有什么帮助吗?

您可以选择具有给定属性的div标签,然后选择所需的标签:

Document doc = ...

final Elements divs = doc.select("div.home-weather-sub-div-bar"); // Select the div-tags

Element firstDiv = divs.get(0); // get the first div
Element secondDiv = divs.get(1); // get the second div


System.out.println(firstDiv.text()); // print the text
System.out.println(secondDiv.text());

使用您发布的html,我得到以下输出:

Sunday, March 3, 2013 updated 14:45:00 CET
Monday, March 4, 2013 until noon

迭代<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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择某些 <tr> 用JSoup标记 使用Jsoup解析HTML div标签 jSoup 将 DIV 标签中的文本提取为字符串 使用jsoup选择一个非重要的div标签 使用带有Java的Jsoup选择div -class标签 去掉 <div> 来自JSoup中html的标记 jsoup根据div标签ID获取值 迭代<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> 在div标签jsoup中获取src属性 如何使用jsoup将特定的div标签替换为span标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM