2014-11-08 123 views
0

打開URL我使用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); 
} 

當一個絕對的URL顯示:

http://www.example.com/blog 

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

/blog 

回答

1

嘗試獲取鏈接的href與此:

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

這會給你一個絕對的URL就可以使用。