- jsoup - Discussion
- jsoup - Useful Resources
- jsoup - Quick Guide
- jsoup - Sanitize HTML
- jsoup - Set Text Content
- jsoup - Set HTML
- jsoup - Set Attributes
- jsoup - Working with URLs
- jsoup - Extract HTML
- jsoup - Extract Text
- jsoup - Extract Attributes
- jsoup - Using Selector Syntax
- jsoup - Using DOM Methods
- jsoup - Loading File
- jsoup - Loading URL
- jsoup - Parsing Body
- jsoup - Parsing String
- jsoup - Environment Setup
- jsoup - Overview
- jsoup - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
jsoup - Working with URLs
Following example will showcase methods which can provide relative as well as absolute URLs present in the html page.
Syntax
String url = "http://www.tutorialspoint.com/"; Document document = Jsoup.connect(url).get(); Element pnk = document.select("a").first(); System.out.println("Relative Link: " + pnk.attr("href")); System.out.println("Absolute Link: " + pnk.attr("abs:href")); System.out.println("Absolute Link: " + pnk.absUrl("href"));
Where
document − document object represents the HTML DOM.
Jsoup − main class to connect to a url and get the html content.
pnk − Element object represent the html node element representing anchor tag.
pnk.attr("href") − provides the value of href present in anchor tag. It may be relative or absolute.
pnk.attr("abs:href") − provides the absolute url after resolving against the document s base URI.
pnk.absUrl("href") − provides the absolute url after resolving against the document s base URI.
Description
Element object represent a dom elment and provides methods to get relative as well as absolute URLs present in the html page.
Example
Create the following java program using any editor of your choice in say C:/> jsoup.
JsoupTester.java
import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; pubpc class JsoupTester { pubpc static void main(String[] args) throws IOException { String url = "http://www.tutorialspoint.com/"; Document document = Jsoup.connect(url).get(); Element pnk = document.select("a").first(); System.out.println("Relative Link: " + pnk.attr("href")); System.out.println("Absolute Link: " + pnk.attr("abs:href")); System.out.println("Absolute Link: " + pnk.absUrl("href")); } }
Verify the result
Compile the class using javac compiler as follows:
C:jsoup>javac JsoupTester.java
Now run the JsoupTester to see the result.
C:jsoup>java JsoupTester
See the result.
Relative Link: index.htm Absolute Link: https://www.tutorialspoint.com/index.htm Absolute Link: https://www.tutorialspoint.com/index.htmAdvertisements