使用 jsoup,我知道如何提取整个 div 的文本:
<div class="c">
<a href="/relurl.php?refid=7">First Anchor Text</a>
Something in Between
<a href="/john.doe?refid=7">Second Anchor Text</a>
</div>
div.text()
产生:
First Anchor Text Something in Between Second Anchor Text
而且我知道如何分别提取每个 anchor 的文本,以便第一个 a.text()
产生:
First Anchor Text
但是在 Jsoup 中是否有一种优雅的方法来提取仅 Something Between
?
(我当然可以提取 2 个 a.text()
并从 div.text()
中“减去”它们,但我不认为这很优雅)
最佳答案
使用 Element#ownText()
.这是链接的 javadoc 的摘录:
ownText
public String ownText()
Gets the text owned by this element only; does not get the combined text of all children.
For example, given HTML
<p>Hello <b>there</b> now!</p>
,p.ownText()
returns"Hello now!"
, whereasp.text()
returns"Hello there now!"
. Note that the text within theb
element is not returned, as it is not a direct child of thep
element.
所以,应该这样做:
String ownText = div.ownText();
// ...
关于java - 包含 a 的 div 中的 Jsoup 文本 "subtraction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6155217/