Jsoup text "subtraction" in a div containing a's
Using jsoup, I know how to extract the text of an entire div:
<di开发者_如何学Pythonv 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>
Such that div.text()
yields:
First Anchor Text Something in Between Second Anchor Text
And I know how to extract the text of each anchor separately, such that the first a.text()
yields:
First Anchor Text
But is there an elegant way in Jsoup to extract only Something in Between
?
(I can of course extract the 2 a.text()
and "subtract" them from div.text()
but I don't consider this elegant)
Use Element#ownText()
. Here's an extract from the linked 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.
So, this should do:
String ownText = div.ownText();
// ...
精彩评论