- 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 - Extracting Text
Overview
Element object represent a dom elment and provides various method to get the text of a dom element.
Syntax
Document document = Jsoup.parse(html);
Element link = document.select("a").first();
System.out.println("Text: " + link.text());
Where
document − document object represents the HTML DOM.
Jsoup − main class to parse the given HTML String.
html − HTML string
link − Element object represent the html node element representing anchor tag.
link.text() − text() method retrives the element text.
Example - Selecting Hyperlinks title
JsoupTester.java
package com.tutorialspoint;
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) {
String html = "<html><head><title>Sample Title</title></head>"
+ "<body>"
+ "<p>Sample Content</p>"
+ "<div id='sampleDiv'><a href='www.google.com'>Google</a>"
+ "<h3><a>Sample</a><h3>"
+"</div>"
+ "<div id='imageDiv' class='header'><img name='google' src='google.png' />"
+ "<img name='yahoo' src='yahoo.jpg' />"
+"</div>"
+"</body></html>";
Document document = Jsoup.parse(html);
//a with href
Elements links = document.select("a[href]");
for (Element link : links) {
System.out.println("Title: " + link.text());
}
}
}
Verify the result
Compile and run the JsoupTester to verify the result −
Title: Google
Example - Getting Text of Divs
JsoupTester.java
package com.tutorialspoint;
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) {
String html = "<html><head><title>Sample Title</title></head>"
+ "<body>"
+ "<p>Sample Content</p>"
+ "<div id='sampleDiv'><a href='www.google.com'>Google</a>"
+ "<h3><a>Sample</a><h3>"
+"</div>"
+ "<div id='imageDiv' class='header'><img name='google' src='google.png' />"
+ "<img name='yahoo' src='yahoo.jpg' />"
+"</div>"
+"</body></html>";
Document document = Jsoup.parse(html);
// select div
Elements divs = document.select("div");
for (Element div : divs) {
System.out.println(div.text());
}
}
}
Verify the result
Compile and run the JsoupTester to verify the result −
Google Sample
Example - Selecting text of paragraph
JsoupTester.java
package com.tutorialspoint;
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) {
String html = "<html><head><title>Sample Title</title></head>"
+ "<body>"
+ "<p>Sample Content</p>"
+ "<div id='sampleDiv'><a href='www.google.com'>Google</a>"
+ "<h3><a>Sample</a><h3>"
+"</div>"
+ "<div id='imageDiv' class='header'><img key='google1' name='google' src='google.png' />"
+ "<img key='yahoo1' name='yahoo' src='yahoo.jpg' />"
+"</div>"
+"</body></html>";
Document document = Jsoup.parse(html);
// select paragraph
Elements paragraphs = document.select("p");
for (Element paragraph : paragraphs) {
System.out.println("Text: " + paragraph.text());
}
}
}
Verify the result
Compile and run the JsoupTester to verify the result −
Text: Sample Content
Advertisements