簡體   English   中英

使用帶有Java的Jsoup選擇div -class標簽

[英]Select div -class tag using Jsoup with Java

我想用Jsoup選擇<div class="article_text">some long text </div>

String url = "computerworld.bg/45781_sofiya_teh_park_tryabva_da_bade_zavarshen_do_kraya_na_2015_g";
Document document = Jsoup.parse(new URL(url).openStream(), "ISO-8859-1", url);
Elements elements = document.select("div.article_text");

然后,我想遍歷元素並獲取theri文本。 但是沒有選擇div。 如果我只嘗試使用div作為css選擇器,則會顯示正確的文本信息,但是還有另一個不合適的divs文本,因此我必須使用類名。

我哪里錯了?

文檔顯然表示還可以。

Element masthead = doc.select("div.masthead").first(); // div with class=masthead

因此,我認為_引起了問題。 嘗試使用div[class=article_text]作為選擇器,如果該方法不起作用,則使用div[class^=article] (類以article開頭),但是它可以選擇比您想要的更多的內容。

更新

div.article_text可在在線Jsoup測試器( http://try.jsoup.org/ )上使用您代碼中的網址進行工作。 也許問題在於您如何獲取文檔。 本示例使用Jsoup.connect()

Document doc = Jsoup.connect("http://www.computerworld.bg/45781_sofiya_teh_park_tryabva_da_bade_zavarshen_do_kraya_na_2015_g").get();

更新2

事實證明,這個特定的url根據用戶代理返回不同的內容(未設置用戶代理,該div上不存在article_text ),因此只需將userAgent設置為Mozilla

Jsoup.connect("http://www.computerworld.bg/45781_sofiya_teh_park_tryabva_da_bade_zavarshen_do_kraya_na_2015_g").userAgent("Mozilla").get();

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

相關問題 使用jsoup選擇一個非重要的div標簽 在Jsoup中選擇特定的div類 如何在Java中使用jSoup獲取span標簽中的所有類屬性 迭代<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> 如何在jsoup中從div中選擇類? Jsoup從div類中選擇一個隨機的(1) 使用Jsoup解析HTML div標簽 使用jsoup在div中選擇標簽的問題 如何使用 jsoup 在 div 中 select 1 跨越 2 使用jsoup選擇div標簽
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM