basfot
basfot

Reputation: 11

How to get absolute URL parh without files

I need get absolute path of links without links to files. I have this code which get me links and some links there missing.

public class Main {

public static void main(String[] args) throws Exception {
    URI uri = new URI("http://www.niocchi.com/");
    printURLofPages(uri);
}

private static void printURLofPages(URI uri) throws IOException {
    Document doc = Jsoup.connect(uri.toString()).get();
    Elements links = doc.select("a[href~=^[^#]+$]");

    for (Element link : links) {
        String href = link.attr("abs:href");
        URL url = new URL(href);
        String path = url.getPath();
        int lastdot = path.lastIndexOf(".");
        if (lastdot > 0) {
            String extension = path.substring(lastdot);
            if (!extension.equalsIgnoreCase(".html") && !extension.equalsIgnoreCase(".htm"))
                return;
        }
        System.out.println(href);
    }
}
}

This code get me following links:

http://www.enormo.com/
http://www.vitalprix.com/
http://www.niocchi.com/javadoc
http://www.niocchi.com/

I need get this links:

http://www.enormo.com/
http://www.vitalprix.com/
http://www.niocchi.com/javadoc
http://www.linkedin.com/in/flmommens
http://www.linkedin.com/in/ivanprado
http://www.linkedin.com/in/marcgracia
http://es.linkedin.com/in/tdibaja
http://www.linkody.com
http://www.niocchi.com/

Thanks a lot for advices.

Upvotes: 0

Views: 128

Answers (1)

bpgergo
bpgergo

Reputation: 16037

instead of

String href = link.attr("href");

try

String href = link.attr("abs:href");

EDIT docs: http://jsoup.org/cookbook/extracting-data/working-with-urls

Upvotes: 1

Related Questions