God
God

Reputation: 1248

Jsoup - Printing element.data() prints nothing

So I'm trying to print the text inside a certain <div> element in a HTML document.

For some reason, when I call the data() method i get an empty console.

public class Program 
{
    public static void main(String[] args) 
    {
        System.out.println("Program starts:");
        try 
        {

            Document document2 = Jsoup.connect("http://www.azlyrics.com/lyrics/eminem/mynameis.html").get();
            Element element3 = document2.select("div.lyricsh").first();

            System.out.println(element3.data());
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    } // End of main method
} // End of Program class

I put the selector in this HTML document http://www.azlyrics.com/lyrics/eminem/mynameis.html. It's on line 150.

What's wrong with my code? Thanks.

Upvotes: 1

Views: 215

Answers (1)

luksch
luksch

Reputation: 11712

Try System.out.println(element3.text());

The data() method is for dataNodes, e.g. if you want to get the inside of a <script> tag.

text() will get the (combined) text of all textNodes within the element. If you want only the text that is attached to the very element in question you can use ownText()

Upvotes: 1

Related Questions