Converting relatively absolute links with jsoup

I am using jsoup to clean up the html page, the problem is when saving the html locally, the images are not displayed because they are all relative links.

Here's some sample code:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class so2 {

    public static void main(String[] args) {

        String html = "<html><head><title>The Title</title></head>"
                  + "<body><p><a href=\"/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif\" target=\"_blank\"><img width=\"437\" src=\"/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif\" height=\"418\" class=\"documentimage\"></a></p></body></html>";
        Document doc = Jsoup.parse(html,"https://whatever.com"); // baseUri seems to be ignored??

        System.out.println(doc);        
    }
}

      

Output:

<html>
 <head>
  <title>The Title</title>
 </head>
 <body>
  <p><a href="/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif" target="_blank"><img width="437" src="/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif" height="418" class="documentimage"></a></p>
 </body>
</html>

      

The output still shows links as a href="/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif"

.

I would like it to convert them to a href="http://whatever.com/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif"

Can anyone show me how to get jsoup to convert all links to absolute links?

+3


source to share


1 answer


You can select all links and convert their hrefs to absolute value with Element.absUrl()

An example in your code:



EDIT (added image processing)

public static void main(String[] args) {

    String html = "<html><head><title>The Title</title></head>"
              + "<body><p><a href=\"/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif\" target=\"_blank\"><img width=\"437\" src=\"/data/abstract/ash/2014/5/9/Paper_69295_abstract_120490_0.gif\" height=\"418\" class=\"documentimage\"></a></p></body></html>";
    Document doc = Jsoup.parse(html,"https://whatever.com"); 

    Elements select = doc.select("a");
    for (Element e : select){
        // baseUri will be used by absUrl
        String absUrl = e.absUrl("href");
        e.attr("href", absUrl);
    }

    //now we process the imgs
    select = doc.select("img");
    for (Element e : select){
        e.attr("src", e.absUrl("src"));
    }

    System.out.println(doc);        
}

      

+3


source







All Articles