我正在使用jsoup解析一些HTML,但我不知道爲什麼我沒有得到我期望的結果。使用Jsoup API:[解析(文件中,字符串charsetName,字符串baseUri)]
Q1:我想要輸出URL爲http://example.com/input/img.jpg
,但它輸出http://example.com/img.jpg
。 Q2:最終的HTML是<img src="/img.jpg">
,但我希望它是<img src="http://example.com/img.jpg">
。
輸入HTML文件
<!-- HTML file -->
<!DOCTYPE html>
<html>
<head>
<title>JsoupInputTest</title>
<meta charset="UTF-8">
</head>
<body>
<div id="mydiv">test parsing input file by jsoup</div>
<img src="/img.jpg">
<a href="/a.jpg">s1 test</a>
</body>
</html>
代碼
public static void inputTest() throws IOException{
String fileName = "../inputTest.html";
File in = new File(fileName);
Document doc = Jsoup.parse(in, "UTF-8", "http://example.com/input/");
System.out.println(doc.select("img").first().absUrl("src"));
System.out.println(doc.select("a[href]").first().absUrl("href"));
System.out.println("====================================");
System.out.println(doc.html());
}
輸出
http://example.com/img.jpg
http://example.com/a.jpg
====================================`enter code here`
<!-- HTML file --><!doctype html>
<html>
<head>
<title>JsoupInputTest</title>
<meta charset="UTF-8">
</head>
<body>
<div id="mydiv">
test parsing input file by jsoup
</div>
<img src="/img.jpg">
<a href="/a.jpg">s1 test</a>
</body>
</html>
@Nathan Van Dyken謝謝你的編輯我的問題 – bluetata