Selecting only the attribute value of a link:href will return the relative URL.
String bodyFragment =
"<div><a href=\"/documentation\">Stack Overflow Documentation</a></div>";
Document doc = Jsoup.parseBodyFragment(bodyFragment);
String link = doc
.select("div > a")
.first()
.attr("href");
System.out.println(link);
Output
/documentation
By passing the base URI into the parse
method and using the absUrl
method instead of attr
, we can extract the full URL.
Document doc = Jsoup.parseBodyFragment(bodyFragment, "http://stackoverflow.com");
String link = doc
.select("div > a")
.first()
.absUrl("href");
System.out.println(link);
Output
http://stackoverflow.com/documentation