使用 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
难道我做错了什么??