开发者

Jsoup text "subtraction" in a div containing a's

开发者 https://www.devze.com 2023-03-09 06:18 出处:网络
Using jsoup, I know how to extract the text of an entire div: <di开发者_如何学Pythonv class=\"c\">

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!", whereas p.text() returns "Hello there now!". Note that the text within the b element is not returned, as it is not a direct child of the p element.

So, this should do:

String ownText = div.ownText();
// ...
0

精彩评论

暂无评论...
验证码 换一张
取 消