2012-07-27 78 views
0

我在使用jsoup獲取圖像的絕對URL時發生錯誤。JSOUP:獲取圖像的絕對URL時出錯

代碼:

package org.zzz.parser; 
import java.io.File; 
import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 

public class DocumentParser { 
    /** 
    * Parsing html from file 
    */ 
    public static void main(String[] args) { 

     String url = "www.guiabh.com.br/evento/back-in-jack-seu-madruga.aspx"; 

     File input = new File("/home/thalesfc/Code/recSystem/wgetao/" + url); 

     Document doc = null; 

     // parsing the document 
     try { 
      doc = Jsoup.parse(input, "ISO8859-1", url); 
     } catch (IOException e) { 
      System.err.println("$ Erro ao fazer o parsing do arquivo: " + input.getName()); 
      e.printStackTrace(); 
     } 

     //getting the image url 
     Element image = doc.getElementById("ctl00_ContentPlaceHolderConteudo_controleInternoAgito1_imageFotoCasa"); 
     String imageUrl = image.attr("src"); 
     String imageRealUrl = image.absUrl("src"); 
     String imageRealUrl2 = image.attr("abs:src"); 

     System.out.println("# image: " + imageUrl); 
     System.out.println("# real image: " + imageRealUrl); 
     System.out.println("# real image 2: " + imageRealUrl2); 
    } 
} 

輸出:

#圖像:../imgs_cadastradas/seu madruga.jpg

#實像:

#實像2:

理想的輸出是:http://www.guiabh.com.br/imgs_cadastradas/seu madruga.jpg

我做錯了什麼?

回答

0

您正在查看「www.guiabh.com.br/evento/back-in-jack-seu-madruga.aspx」網址。

當你得到圖像url時,它以「../」開頭,這意味着要擊中一個lvl。

簡而言之,它可以追溯到「www.guiabh.com.br/evento/」+圖片網址。

它引發錯誤,因爲你正試圖在這裏連接:「../imgs_cadastradas/seu madruga.jpg」

我avise有一個方法來獲得從哪裏獲得的圖像的URL ,而不是預測我上面所說的。