Skip to content

Jsoup get absolute URL example

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 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 URLs using base URI.

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

Output

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.

As a standard practice you should always use the HTTP user agent and referer values while making the Jsoup request.

This example is a part of the Jsoup tutorial with examples.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.