jsoup - Loading URL



Overview

Jsoup.connect(url) method makes a connection to the url and Jsoup.get() method return the html of the requested url.

Syntax

String url = "http://www.google.com";
Document document = Jsoup.connect(url).get();

Where

  • document − document object represents the HTML DOM.

  • Jsoup − main class to parse the given HTML String.

  • url − url of the html page to load.

Get the data using document object

Element body = document.body();

Here body represents element children of the document's body element and is equivalent to document.getElementsByTag("body").

Read tag values

Elements divs = body.getElementsByTag("div");
for (Element div : divs) {
   System.out.println(div.text());
}

Example - Connecting and loading HTML title

JsoupTester.java

package com.tutorialspoint;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsoupTester {
   public static void main(String[] args) throws IOException {
   
      String url = "http://www.google.com";
      Document document = Jsoup.connect(url).get();
      System.out.println("Title: " + document.title());
   }
}

Verify the result

Compile and run the JsoupTester to verify the result −

Title: Google

Example - Connecting and loading HTML Body

JsoupTester.java

package com.tutorialspoint;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupTester {
   public static void main(String[] args) throws IOException {
   
      String url = "http://www.google.com";
      Document document = Jsoup.connect(url).get();
      Element body = document.body();
      Elements divs = body.getElementsByTag("div");
      for (Element div : divs) {
         System.out.println(div.text());
      }
   }
}

Verify the result

Compile and run the JsoupTester to verify the result −

AboutStore Gmail Images Sign in AI Mode 
...
Advertisements