【发布时间】:2023-03-31 23:37:01
【问题描述】:
我正在尝试用 Java 构建一个 Web Crawler,我想知道是否有任何方法可以从给定基本 url 的绝对路径中获取相对路径。我正在尝试替换同一域下 html 中的任何绝对路径。
由于 http url 包含不安全的字符,我无法使用 How to construct a relative path in Java from two absolute paths (or URLs)? 中描述的 Java URI。
我正在使用 jsoup 来解析我的 html,它似乎能够从相对路径获取绝对路径,但反之则不行。
例如 在以下html的特定html中,
"http://www.example.com/mysite/base.html"
在base.html的页面源码中,可以包含:
'<a href="http://www.example.com/myanothersite/new.html"> Another site of mine </a>
我正在尝试缓存这个 base.html,并对其进行编辑,使其现在包含:
'<a href="../myanothersite/new.html">Another site of mine</a>
【问题讨论】:
-
那么,您以“example.com/mysite/whatever”为基础,并希望拥有以它开头的所有网站,相对于它?或者相对于什么?
-
是的。基本上,我想将该特定 html 中的所有绝对 url 更改为使用该特定 html url 作为基础的相对 url。
-
请重新审视我对您的问题的猜测。
-
请看我编辑的答案。
标签: java url web-crawler jsoup