- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 jsoup 来解析一些 HTML,但我不知道为什么我没有得到我期望的结果。
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 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>
最佳答案
Jsoup作者Jhy的回答:#908
Q1: both of those are absolute URLs with implicit domains -- the URL starts with a single slash. So only the hostname from the base HREF (the third argument to Jsoup.parse) is used. The output is correct.
See https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL#Absolute_URLs_vs_relative_URLs for some more details on how URLs are made absolute.
Q2: calling absUrl() doesn't change the value in the DOM; it's calculating the absolute URL, not updating it.
关于java - 使用 Jsoup API : [parse(File in, String charsetName, String baseUri)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44847302/
通常,当我需要将字符串转换为 byte[] 时,我使用不带参数的 getBytes() 。我检查过它没有保存我应该使用字符集。为什么我应该这样做 - 字母“A”总是会被解析为0x41?不是吗? 最佳答
我正在尝试使用 JDK11 提供的 JShell 来运行一些简单的命令。但是当我输入时: jshell>System.out.println("Hello World!"); 它给我错误: Excep
当我尝试为我的 WAR 项目运行命令 mvn archetype:create-from-project 时,在读取 .java 文件后会显示此异常: [ERROR] Failed to execut
我有一个关于 Charset.forName(String charsetName) 的问题。有我可以引用的字符集名称列表吗?例如,对于 UTF-8,我们使用“utf8”作为字符集名称。 WINDOW
我需要使用 UTF-8 编码将字符串编码为字节数组。我正在使用 Google guava,它的 Charsets 类已经为 UTF-8 编码定义了 Charset 实例。我有两种方法: String.
我正在使用 jsoup 来解析一些 HTML,但我不知道为什么我没有得到我期望的结果。 Q1:我希望输出 URL 为 http://example.com/input/img.jpg ,但它输出 ht
我是一名优秀的程序员,十分优秀!