简体   繁体   English

Jsoup只选择div中的文本和其他元素

[英]Jsoup selecting only text within a div with other elements

I have div with a lot of inner mumbo jumbo like this: 我有很多内在的mumbo jumbo这样的div

<div>sometext

   <p>

   </p>sometext

   <div>

   </div>sometext

</div>

and I need to choose the text that is in the parent div without all the text in the children elements. 我需要选择父div的文本而不包含子元素中的所有文本。 I cannot use .not() to get rid of the divs inside. 我不能使用.not()来摆脱里面的div。 I cannot get the children elements either because it eliminates the text inside and returns only the inside elements. 我无法获取子元素,因为它消除了内部文本并仅返回内部元素。 So basically I need the text only without any of the children elements. 所以基本上我只需要没有任何子元素的文本。

How do I do that? 我怎么做?

Actually you need to use the yourElement.ownText(); 实际上你需要使用yourElement.ownText();

as the Jsoup's doc states: 正如Jsoup的文件所述:

String ownText() : Gets the text owned by this element only; String ownText() :仅获取此元素拥有的文本; does not get the combined text of all children. 没有得到所有孩子的组合文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM