2013-09-30 139 views
1

我想在Java中构建Web爬网程序,并且我想知道是否有任何方法可以从给定基础的绝对路径获取相对路径网址。我试图在同一个域下的html中替换任何绝对路径。如何从Java中的绝对http路径获取相对路径

由于http urls包含不安全字符,因此我无法使用如在How to construct a relative path in Java from two absolute paths (or URLs)?中所述的Java URI。

我使用jsoup来解析我的html,它似乎能够从相对,而不是相反的方式获得绝对路径。

E.g. 在下面的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> 
+0

因此,你有“http://www.example.com/mysite/whatever”作为基础,并希望所有的网站开始与它相关?还是相对于什么? –

+0

是的。基本上我想改变这个特定的html中的所有绝对URL,以使用该特定的HTML网址作为基础来成为相对的URL。 – Wee

+0

请重温我对你想要的问题的猜测。 –

回答

2

一种不需要给定baseUrl并使用更高级方法的不同方法。

String sourceUrl = "http://www.example.com/mysite/whatever/somefolder/bar/unsecure!+?#whätyöühäv€it/site.html"; // your current site 
    String targetUrl = "http://www.example.com/mysite/whatever/otherfolder/other.html"; // the link target 
    String expectedTarget = "../../../otherfolder/other.html"; 
    String[] sourceElements = sourceUrl.split("/"); 
    String[] targetElements = targetUrl.split("/"); // keep in mind that the arrays are of different length! 
    StringBuilder uniquePart = new StringBuilder(); 
    StringBuilder relativePart = new StringBuilder(); 
    boolean stillSame = true; 
    for(int ii = 0; ii < sourceElements.length || ii < targetElements.length; ii++) { 
     if(ii < targetElements.length && ii < sourceElements.length && 
       stillSame && sourceElements[ii].equals(targetElements[ii]) && stillSame) continue; 
     stillSame = false; 
     if(targetElements.length > ii) 
      uniquePart.append("/").append(targetElements[ii]); 
     if(sourceElements.length > ii +1) 
      relativePart.append("../"); 
    } 

    String result = relativePart.toString().substring(0, relativePart.length() -1) + uniquePart.toString(); 
    System.out.println("result: " + result); 
0

这应该这样做。请记住,您可以通过测量源目标网址和目标网址相同的距离来计算baseUrl!

String baseUrl = "http://www.example.com/mysite/whatever/"; // the base of your site 
    String sourceUrl = "http://www.example.com/mysite/whatever/somefolder/bar/unsecure!+?#whätyöühäv€it/site.html"; // your current site 
    String targetUrl = "http://www.example.com/mysite/whatever/otherfolder/other.html"; // the link target 
    String expectedTarget = "../../../otherfolder/other.html"; 
    // cut away the base. 
    if(sourceUrl.startsWith(baseUrl)) 
     sourceUrl = sourceUrl.substring(baseUrl.length()); 
    if(!sourceUrl.startsWith("/")) 
     sourceUrl = "/" + sourceUrl; 

    // construct the relative levels up 
    StringBuilder bar = new StringBuilder(); 
    while(sourceUrl.startsWith("/")) 
    { 
     if(sourceUrl.indexOf("/", 1) > 0) { 
      bar.append("../"); 
      sourceUrl = sourceUrl.substring(sourceUrl.indexOf("/", 1)); 
     } else { 
      break; 
     } 
     System.out.println("foo: " + sourceUrl); 
    } 

    // add the unique part of the target 
    targetUrl = targetUrl.substring(baseUrl.length()); 
    bar.append(targetUrl); 

    System.out.println("expectation: " + expectedTarget.equals(bar.toString())); 
    System.out.println("bar: " + bar); 
相关问题