且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用java在网页中查找超链接?

更新时间:2023-12-06 15:48:40

尝试使用 jsoup库

下载项目jar并编译此代码片段:

Download the project jar and compile this code snippet:

    Document doc = Jsoup.parse(new URL("http://www.bits4beats.it/"), 2000);

    Elements resultLinks = doc.select("a");
    System.out.println("number of links: " + resultLinks.size());
    for (Element link : resultLinks) {
        System.out.println();
        String href = link.attr("href");
        System.out.println("Title: " + link.text());
        System.out.println("Url: " + href);
    }

代码打印html页面中的超文本元素数量及其相关信息。

The code prints the numbers of hypertext elements in a html page and infos about them.