How to extract absolute URL from relative HTML links using Jsoup?

后端 未结 2 936
野的像风
野的像风 2020-12-05 18:36

I am using Jsoup to extract URL of an webpage. The href attribute of those URL\'s are relative like:

example
<         


        
2条回答
  •  甜味超标
    2020-12-05 19:08

    You need Element#absUrl().

    String url = dl.select("a").absUrl("href");
    

    You can by the way shorten the select:

    Document document = Jsoup.connect(url).get();
    Elements links = document.select("div.results dl a");
    for (Element link : links) {
        String url = link.absUrl("href");
    }
    

提交回复
热议问题