Search code examples
javarestjsoup

Jsoup parse response without logging to console


I have a tiny problem with Jsoup.
I am using this snippet of code

        Response response = getAccount(); // GET Response in HTML format
        Document document = Jsoup.parse(response.body().prettyPrint());

And it prints to console all the response, which is very messy, as response is in HTML format. I read that prettyPeek() is not logging response, but return value of prettyPeek() is not String type but Response, and even if I use prettyPeek().toString() my code doesn't work. Please tell me what snippet will work the same way as mine, but without logging to console.


Solution

  • To parse HTML into a Document just parse the body:

     Document document = Jsoup.parse(response.body());
    

    And that's it.
    Also do you really need a reponse as a Response object? You can get the Document by simply calling:

    Document document = Jsoup.connect("http://example.com/").get();
    

    Take a look at these very simple examples to see if there's a better way to do what you're trying to achieve: https://jsoup.org/cookbook/input/load-document-from-url