簡體   English   中英

用相對路徑打開URL

[英]Open url with relative path

我正在使用Jsoup從網站提取鏈接:

String domain_url = "http://www.example.com";
Document doc;

doc = Jsoup.connect(domain_url)
    .userAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
    .timeout(5000)
    .get();

Elements links = doc.select("a[href]");
for (Element link : links)
{
    String link_href = link.attr("href");
    System.out.println(link_href);
}

當出現絕對網址時:

http://www.example.com/blog

調用該網址沒有問題。 但是如何打開相對鏈接?

/blog

嘗試通過以下方式獲取鏈接href:

String link_href = link.absUrl("href");

這將為您提供一個可以使用的絕對URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM