Jsoup:从锚标记中提取无限文本 [英] Jsoup: Extracting innertext from anchor tag

查看:104
本文介绍了Jsoup:从锚标记中提取无限文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题。我有一个html内容:


innerText


我需要提取innerText。在Jsoup中试用时,我发现当由Jsoup解析时,innertext会超出anchor标记。

Here's my problem. I have a html content: innerText I need to extract the "innerText". While trying this in Jsoup I found that the innertext goes outside the anchor tag when parsed by Jsoup.

这是我的代码

Document doc=Jsoup.parse("<div>  <a href="#"> innerText  </a> </div>");
System.out.println(doc.html());

输出:

output:

<html>
 <head></head>
 <body>
  <div >
   <a href="#"></a>innerText
  </div>
 </body>
</html>

为什么innerText会移出锚标签?

why is "innerText" moved outside the anchor tag?

推荐答案

您可以通过调用 text()方法来访问文本

You can access the text by calling the text()method on the element.

Document doc = Jsoup.parse("<div>  <a href=\"#\"> innerText  </a> </div>");
System.out.println(doc.html());
Elements rows = doc.getElementsByTag("a");
for (Element element : rows) {
    System.out.println("element = " + element.text());
}

btw。使用您的发布代码(和JSoup 1.8.1)会生成以下输出:

btw. Using your posted code (and JSoup 1.8.1) produces the following output

<html>
    <head></head>
    <body>
        <div> 
            <a href="#"> innerText </a> 
        </div>
    </body>
</html>

这篇关于Jsoup:从锚标记中提取无限文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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