Jsoup get absolute URL example shows how to get absolute URL from relative URL found in link href attribute or image src attribute using absURL
method of element class.
How to get the absolute URL from the relative URL using Jsoup?
Many of the websites load various resources like images and CSS using the relative URLs. If you want to download these resources as well using Jsoup, you need to convert these relative URLs to the absolute URLs.
Jsoup element class provides absUrl
method which converts relative URLs to absolute URL using base URI.
1 | public String absURL(String attributeName) |
If the URL is already an absolute URL, it is returned as-is. If it is a relative URL, it is converted to an absolute URL using the base URI and returned.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.javacodeexamples.libraries.jsoup; 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 JsoupGetAbsoluteURLExample { public static void main(String[] args) { try{ Document document = Jsoup .connect("http://www.google.com") .userAgent("Mozilla/5.0") .timeout(10 * 1000) .get(); Elements elements = document.select("img[src]"); for(Element e : elements){ System.out.println("Relative URL: " + e.attr("src")); System.out.println("Absolute URL: " + e.absUrl("src")); } }catch(IOException ioe){ System.out.println("Exception: " + ioe); } } } |
Output
1 2 | Relative URL: /images/icons/product/chrome-48.png Absolute URL: http://www.google.co.in/images/icons/product/chrome-48.png |
You can also use the attr
method with the “abs:” prefix with attribute name to get the absolute URL instead of the absUrl
method as given below.
1 | System.out.println("Absolute URL: " + e.attr("abs:src")); |
This example is a part of the Jsoup tutorial with examples.
Please let me know your views in the comments section below.
Add Comment