從下面的示例HTML代碼中提取href值是直線前進,如果我遍歷所有,第一個後,立即打破:提取內部元件不循環
<li class="parts partname parts_first">
<div id="dpdn10" uri="/public/page/part1" class="partype partstate">
<div class="ptctainer">
<div class="ptitle">
<p class="ptypead">
<span class="rtext"><a href="http://www.example.com/page/ptname.html?dv=rfirst" class="mnLabel">First</a></span>
<span class="ndx">
<a href="#" dndx="dpdn10" class="xpnd _t" style="opacity:1">Details: </a>
</span>
</p>
</div>
</div>
<div id="dpdn10_content" class="xpns">
<div class="ptctainer">
<div class="ptitle">
<p class="ptypead">
<span class="rtext"><a href="http://www.example.com/page/ptname.html?dv=rfirst" class="mnLabel">First</a></span>
<span class="ndx"><a href="#" class="xpnd">Details: </a></span>
</p>
</div>
</div>
</div>
</div>
</li>
我肯定能做到這一點,當我可以假設對於的兩個實例href值都是相同的,如上例所示。
但是,如果它們不相同並且我想提取特定的一個(第一個或第二個),則此方法會失敗。
這使我尋找在Jsoup一種機制,允許「嵌套的選擇」:到現在爲止,我已經熟悉了單層次的選擇,如下所示:
Elements links = doc.select("a[href]"); // a with href
Elements pngs = doc.select("img[src$=.png]"); // img with src ending .png
Element masthead = doc.select("div.masthead").first(); // div with class=masthead
但我不能找到文檔或多級選擇的例子,例如
Element link= doc.select("div.xpns.div.ptctainer.div.ptitle.p.ptypead.span.rtext");
以上僅用於說明而不是真正的語法,當然。我不知道在Jsoup中是否有這樣的可能。
在Jsoup中是否存在這種「嵌套選擇」?
當然,我可以但「鏈接」形式意味着較少的優化遍歷/解析。我希望Jsoup能夠以最佳方式處理這種情況。感謝和+1。 – ef2011 2011-04-24 21:23:56
分析總是很慢。理想情況下,你會一起跳過它。 – Halcyon 2011-04-24 22:00:21
我曾希望Jsoup擁有類似於它已有的[祖先子](http://jsoup.org/cookbook/extracting-data/selector-syntax)的多級機制。但似乎還沒有。 – ef2011 2011-04-26 11:14:42