Jsoup: Extracting innertext from anchor tag

San*_*van 5 html java html-parsing jsoup

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.

Here's my code

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

output:

<html>
 <head></head>
 <body>
  <div >
   <a href="#"></a>innerText
  </div>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

why is "innerText" moved outside the anchor tag?

Sub*_*mal 5

text()您可以通过调用元素上的方法来访问文本。

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());
}
Run Code Online (Sandbox Code Playgroud)

顺便提一句。使用您发布的代码(和 JSoup 1.8.1)会产生以下输出

<html>
    <head></head>
    <body>
        <div> 
            <a href="#"> innerText </a> 
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)