我抓取一个网站并在页面上找到这些链接:
index.html
bla.html
/index.html
A.com/test.html
http://wwww.B.com/bla.html
如果我知道当前页面是www.A.com/some/path,如何有效地将这些链接转换为“真实网址”。因此,在每种情况下,网址应转换为:
index.html => http://www.A.com/some/path/index.html
bla.html => http://www.A.com/some/path/bla.html
/index.html => http://www.A.com/index.html
A.com/test.html => http://www.A.com/test.html
http://wwww.B.com/bla.html => http://wwww.B.com/bla.html
将这些页面链接转换为其完全限定的网址名称的最有效方法是什么?
最佳答案
使用java.net.URL
类:
URL BASE_PATH = new URL("http://www.A.com/some/path");
String RELATIVE_PATH = "index.html";
URL absolute = new URL(BASE_PATH, RELATIVE_PATH);
它将根据基本路径解析相对 URL。如果相对 URL 实际上是绝对 URL,它将返回它。
关于java - 将抓取的 URL 转换为真实 URL 最安全的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26824112/