Java 处理在HTML页面中找到的URL
我有一个Java 处理在HTML页面中找到的URL,java,html,url,Java,Html,Url,我有一个HTML页面。我使用jsoup解析它,下面是代码的一部分 Document doc = Jsoup.parse(content); org.jsoup.select.Elements images = doc.select("[src]"); for(org.jsoup.nodes.Element img : images) { // Here I need to determine the type of the URL and convert it to absolute
HTML
页面。我使用jsoup
解析它,下面是代码的一部分
Document doc = Jsoup.parse(content);
org.jsoup.select.Elements images = doc.select("[src]");
for(org.jsoup.nodes.Element img : images) {
// Here I need to determine the type of the URL and convert it to absolute URL
}
我需要将此HTML中的所有URL更改为绝对URL。
问题是,
的src
属性可能是类型,如果主机ifwww.example.com
:
1. http://www.example.com/images/1.png
2. http://example.com/images/1.png
3. www.example.com/images/1.png
4. example.com/images/1.png
5. /example.com/images/1.png
6. //example.com/images/1.png
7. /images/1.png
我在测试时提出了这个列表,所以我应该支持它们。所以我需要一些函数来输出绝对URL(http://www.example.com/images/1.png
)用于上面列出的所有输入。
当URL用于另一个位置的资源时,问题甚至变得复杂,例如haha.com/images/1.png
所以我需要一种方法来确定URL的类型,比如:
- 相对(
)李>/images/1.png
- 绝对(
)李>http://example.com/images/1.png
- 协议相关(
)example.com/images/1.png
用java解决这个问题的最佳方法是什么?谢谢。查看DOM中可用的方法。特别是:document.URL