Jsoup’s connect method

In my previous post under Jsoup, I showed with example how to parse an html document located on world wide web using overloaded “parse” method.

In this post, I will show with example another approach of doing the same thing.

The Jsoup javadoc recommends to follow this approach instead of using overloaded “parse” method.

Below is the complete code of the example

Main Class

1  import java.io.IOException;
3  import org.jsoup.Connection;
4  import org.jsoup.Jsoup;
5  import org.jsoup.nodes.Document;
7  public class JsoupDemo3 {
8      public static void main(String[] args) throws IOException {
9          Connection connection = Jsoup.connect("http://www.google.com");
10         Document document = connection.get();
11         System.out.println(document.title());
12     }
13 }

In the above code, at line 9, I use the static “connect” method passing the url of Google website as an argument.

The return value of this method is an instance of Connection class.

At line 10, using the Connection object we call “get” method to get the Document object representing the parsed html.

In this way we can parse an html document located on world wide web.

Leave a Reply