简体   繁体   English

使用jsoup选择div标签

[英]selecting a div tag with jsoup

Hi I have 2 divs both displaying dates and having the same name. 嗨,我有2个div既显示日期又具有相同的名称。 I want to specifically display the first div for now, the second date for later on. 我现在要专门显示第一个div,以后再显示第二个日期。 I have tried searching but what I found is that you can specify the div name element and distinguish between both divs by it. 我尝试搜索,但是发现可以指定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>

So to be clear, I have tried to select "div.home-weather-sub-div-bar" and "span.datetext1" yet it will still display Sunday, March 3, 2013 updated 14:45:00 CET Monday and then it will concatenate also March 4, 2013 until noon in the same line. 为了清楚起见,我尝试选择“ div.home-weather-sub-div-bar”和“ span.datetext1”,但仍会显示2013年3月3日(星期日),CET星期一更新14:45:00,然后它也将在2013年3月4日连接到同一行中午。

Any help please? 有什么帮助吗?

You can select the div tags with given attributes and then choose which one you want: 您可以选择具有给定属性的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());

With the html you've posted i get this output: 使用您发布的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标记 - Selecting certain <tr> tag with JSoup 使用Jsoup解析HTML div标签 - Parsing HTML div tag using Jsoup jSoup 将 DIV 标签中的文本提取为字符串 - jSoup extract Text out of DIV tag to String 使用jsoup选择一个非重要的div标签 - Select a non significant div tag using jsoup 使用带有Java的Jsoup选择div -class标签 - Select div -class tag using Jsoup with Java 去掉 <div> 来自JSoup中html的标记 - remove <div> tag from html in JSoup jsoup根据div标签ID获取值 - jsoup get values based on div tag 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> - Iterate <div> inside <ul> tag Java - Jsoup 在div标签jsoup中获取src属性 - get src attribute inside div tag jsoup 如何使用jsoup将特定的div标签替换为span标签 - how to replace a particular div tag with a span tag using jsoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM