URL收割机操纵



我正在做一个递归URL收获。当我在源中找到一个不以" HTTP"开头的链接时,我将其附加到当前的URL。问题是当我碰到动态站点时,没有HTTP的链接通常是当前URL的新参数。例如,如果当前URL类似于http://www.somewebapp.com/default.aspx?pageID=4088,则在该页面的源中,有一个link default.aspx?appx?pageid = 2111。在这种情况下,我需要进行一些字符串操作;这是我需要帮助的地方。
伪代码:

if part of the link found is a contains a substring of the current url
      save the substring            
      save the unique part of the link found
replace whatever is after the substring in the current url with the unique saved part

Java中的样子是什么样的?对这样做的想法有所不同吗?谢谢。

根据评论,这是我尝试的:

if (!matched.startsWith("http")) {
    String[] splitted = url.toString().split("/");
    java.lang.String endOfURL = splitted[splitted.length-1];
    boolean b = false;
    while (!b && endOfURL.length() > 5) { // f.bar shortest val
        endOfURL = endOfURL.substring(0, endOfURL.length()-2);
        if (matched.contains(endOfURL)) {
            matched = matched.substring(endOfURL.length()-1);
            matched = url.toString().substring(url.toString().length() - matched.length()) + matched;
            b = true;
        }
    }

它工作不好。

我认为您以错误的方式这样做。Java有两个类URLURI类,它们能够比"串行敲击"解决方案更准确地解析URL/URL字符串。例如,URL构造函数URL(URL, String)将在现有的上下文中创建一个新的URL对象,而无需担心字符串是绝对的URL还是相对的URL。您将使用类似的东西:

URL currentPageUrl = ...
String linkUrlString = ...
// (Exception handling not included ...)
URL linkUrl = new URL(currentPageUrl, linkUrlString);

最新更新