Jsoup: how to get an image's absolute url?

后端 未结 4 1636
难免孤独
难免孤独 2020-12-01 02:42

Is there a way in jsoup to extract an image absolute url, much like one can get a link\'s absolute url?

Consider the following image element found in http://ww

4条回答
  •  感动是毒
    2020-12-01 03:14

    Let's assume you are parsing http://www.example.com/index.html.

    Use jsoup to extract the img src which gives you: images/chicken.jpg

    You can then use the URI class to resolve this to an absolute path:

    URL url  = new URL("http://www.example.com/index.html");
    URI uri = url.toURI();
    System.out.println(uri.resolve("images/chicken.jpg").toString());
    

    prints

    http://www.example.com/images/chicken.jpg
    

提交回复
热议问题