如何使用jsoup获取不属于任何元素的文本? [英] How to get text which is not part of any element using jsoup?

查看:225
本文介绍了如何使用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  "

这篇关于如何使用jsoup获取不属于任何元素的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆