Skip to content

Instantly share code, notes, and snippets.

@revox
Created March 9, 2015 11:12
Show Gist options
  • Save revox/37a930146a26de125189 to your computer and use it in GitHub Desktop.
Save revox/37a930146a26de125189 to your computer and use it in GitHub Desktop.
JSoup using abs:href to get absolute URLs
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class JSoupExampleThree {
public static void main(String[] args) throws Exception {
Document doc = Jsoup.connect("http://gold.ac.uk").get();
Elements links = doc.getElementsByTag("a");
String relHref = links.get(1).attr("href"); // == "/"
System.out.println(relHref);
String absHref = links.get(1).attr("abs:href"); // "http://jsoup.org/"
System.out.println(absHref);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment