如何使用jsoup获取不属于任何元素的文本?

如何使用jsoup获取不属于任何元素的文本?

问题描述:

如何获取不属于任何元素的文本?

How to get the text which is not part of any element?

<br><b>Price:</b> &nbsp; Rs. 24,900.00 &nbsp; <br>

在这里,如何获得文本Rs.24,900.00.可以使用jsoup吗?

Here, how can one get the text Rs.24,900.00. Is this possible using jsoup?

我想有一个父元素,因此您应该先选择该元素,然后再选择"b",如下面的代码.基本上只是在文本前面找到元素.

I suppose there is a parent element so you should select that first and after just select the "b" like the following code. Basically just find the element in front of your text.

Document doc = Jsoup.parse( "<br><b>Price:</b> &nbsp; Rs. 24,900.00 &nbsp; <br>");
Element el = doc.select("b").first();
String text = ((TextNode) el.nextSibling()).text();

我之所以首先使用它是因为我从您的示例中知道只有一个"b"元素.如果您有多个价格,则必须遍历所有元素,而不要先使用.

I used first because I knew from your example that there is only one "b" element. In case you have multiple prices you have to iterate over all elements instead of using first.

Jsoup将文本存储为节点.因此,nextSibling将返回一个节点(TextNode),该节点位于"b"元素之后,并包含文本值:  Rs.24,900.00 "

Jsoup stores text as nodes. So nextSibling will return a node (TextNode) that follows after the "b" element and contains text value: "  Rs. 24,900.00  "