使用jsoup标记后获取特定文本 [英] Getting certain text after tag with jsoup

查看:796
本文介绍了使用jsoup标记后获取特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用jsoup从HTML文件中获取特定文本。我已经知道如何获得text2和text3。但是,如何获得我想要的文本而不需要其他人?我想要
< br clear =both/> text2
< br clear =both/> text3
< br clear =both/>
< / div>

我试过使用

 元素行= doc.select(。snt); 
lines.First()。nextSibling()。toString();

但我什么也没得到。我也试过:

  Elements lines = doc.select(。snt); 
lines.text(); //返回所有文本

你能帮我吗?如果你尝试使用ownText()作为第一个元素,你会得到text我想要text2 text3 这是正确的。你需要br之前的文本,这是你的第一个元素下的第一个子节点。 Jsoup将文本处理为一个节点。

 元素行= doc.select(。snt); 
System.out.println(lines.first()。childNodes()。get(0));


Hi I am trying to get certain text from HTML file using jsoup. I already know how to get text2 and text3. But how do I get text I want without others?

<div class="snt"> text I want  
<br clear="both" />text2  
<br clear="both" />text3  
<br clear="both" />  
</div>    

I tried to use

Elements lines = doc.select(".snt");
lines.First().nextSibling().toString();    

but I get nothing. I also tried:

Elements lines = doc.select(".snt");  
lines.text(); // this return all texts together       

Can you please help me? Thank you for your answers.

解决方案

If you try ownText() for the first element you will get "text I want text2 text3" and this is correct. You want the text before br and this is the first child node under your first element. Jsoup handles text as a node.

Elements lines = doc.select(".snt");
System.out.println(lines.first().childNodes().get(0));

这篇关于使用jsoup标记后获取特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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