Search code examples
javajsoup

How can I read into a webpage non-linked text with jsoup?


I know, if I would like to print links and texts of links with Jsoup I have to use this code:

        Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/Jsoup").get();
        Elements links = doc.select("a[href]");
        for (Element link : links) {
            System.out.println(link.attr("abs:href") + " - " + link.text());
        }

Output: (Not the complete)

   https://en.wikipedia.org/wiki/Jsoup#mw-head - Jump to navigation
   https://en.wikipedia.org/wiki/Jsoup#p-search - Jump to search
   https://en.wikipedia.org/wiki/Software_developer - Developer(s)
   https://en.wikipedia.org/wiki/Software_release_life_cycle - Stable release 
   https://en.wikipedia.org/wiki/Jsoup#cite_note-1 - [1]
   https://en.wikipedia.org/wiki/Jsoup#cite_note-2 - [2]
   https://en.wikipedia.org/wiki/Repository_(version_control) - Repository 
   https://github.com/jhy/jsoup - github.com/jhy/jsoup
   ...

And if I would like to print texts of entire webpage I have to use this code:

System.out.println(doc.body().text());

Output: (Not the complete)

jsoup From Wikipedia, the free encyclopedia Jump to navigation Jump to search jsoup Java HTML Parser Developer(s) Jonathan Hedley Stable release 1.11.3 [1] / 2018-04-15 [2] Repository github.com/jhy/jsoup...

How can I print the all text of doc without text of links?

Output what I wish:

jsoup From Wikipedia, the free encyclopedia jsoup Java HTML Parser Jonathan Hedley 1.11.3 / 2018-04-15 ...


Solution

  • Jsoup's Elements has a remove() method. That should remove the links from your document.

    Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/Jsoup").get();
    doc.select("a[href]").remove();
    System.out.println(doc.body().text());