- jsoup - Home
- jsoup - Overview
- jsoup - Environment Setup
- Examples - Input
- jsoup - Parsing String
- jsoup - Parsing Body
- jsoup - Loading URL
- jsoup - Loading File
- Examples - Extracting Data
- jsoup - Using DOM Methods
- jsoup - Using Selector Syntax
- jsoup - Extract Attributes
- jsoup - Extract Text
- jsoup - Extract HTML
- jsoup - Working with URLs
- Examples - Modifying Data
- jsoup - Set Attributes
- jsoup - Set HTML
- jsoup - Set Text Content
- Examples - Cleaning HTML
- jsoup - Sanitize HTML
- jsoup Useful Resources
- jsoup - Quick Guide
- jsoup - Useful Resources
- jsoup - Discussion
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