如何使用Jsoup解析内外部超链接

How to use the Jsoup to parse internal and external hyperlinks

我正在练习网页抓取,我想知道如何才能只抓取内部和外部超链接。我可以按照 Jsoup 文档检索页面的所有链接,但我仍然不知道如何执行此操作。

提前感谢您的帮助

您可以尝试使用 Document.getElementsByAttributeValueMatching() 方法使用正确的正则表达式按属性名称和属性值查找元素。

例如,查找属性名称为“href”且属性值以https://example.com

开头的所有元素
    Document document = Jsoup.connect("https://example.com").get();
    Elements elements = document.getElementsByAttributeValueMatching("href", "^https://example.com");

    for (Element element : elements) {
        System.out.println(element.attr("href"));
    }

还有一些类似的方法:

Document.getElementsByAttributeValueStarting()
Document.getElementsByAttributeValueContaining()