如何使用 Java 从网页中提取所有链接(相对和绝对)?

How to extract all links (relative and absolute) from a webpage using Java?

我正在尝试使用 jSoup 提取并显示网页上的所有链接:

Document doc =   Jsoup.connect("https://www.youtube.com/").get();
   Elements links = doc.select("link");
    Elements scripts = doc.select("script");
   for (Element element : links) {
         System.out.println("href:" + element.absUrl("href"));
   }
   for (Element element : scripts) {
         System.out.println("src:" + element.absUrl("src"));

这是我的代码。但是它没有错误地工作,它没有给我所有的链接,而是只有几个。许多 src 元素被输出为空白。这是输出:

herehref:https://s.ytimg.com/yts/cssbin/www-core-vfluKFg1a.css
here`href:https://s.ytimg.com/yts/cssbin/www-home-c4-vfl4p1Pju.css
href:https://s.ytimg.com/yts/cssbin/www-pageframe-vflfdzMKI.css
href:https://s.ytimg.com/yts/cssbin/www-guide-vflTkT47C.css
href:http://www.youtube.com/opensearch?locale=en_US
href:https://s.ytimg.com/yts/img/favicon-vfldLzJxy.ico
href:https://s.ytimg.com/yts/img/favicon_32-vflWoMFGx.png
href:http://www.youtube.com/
href:https://m.youtube.com/?
href:https://m.youtube.com/?
href:https://plus.google.com/115229808208707341778
src:
src:
src:https://s.ytimg.com/yts/jsbin/www-scheduler-vflNAje0j/www-scheduler.js
src:
src:
src:https://s.ytimg.com/yts/jsbin/spf-vfld6zcp2/spf.js
src:https://s.ytimg.com/yts/jsbin/www-en_US-vflLgbz4u/base.js
src:
src:

请告诉我为什么会发生这种情况以及如何纠正它?

当您想要 select 通过 a 元素的所有超链接时,您正在 select 所有 link 元素。

script 元素的空白输出是由于某些元素没有指向具有 src 属性的外部脚本源,而是保持内联 javascript 语句。

您可以使用不同的 selector 获取具有该属性的那些元素的 src,如下所示。

//Get the document
Document doc =   Jsoup.connect("https://www.youtube.com/").get();

//Get all the hyperlinks
Elements links = doc.select("a[href]");
//Loop through them
for (Element element : links) {
     System.out.println("href: " + element.absUrl("href"));
}

//Get all script elements with src
Elements scriptSources = doc.select("[src]");
//Loop through them
for (Element element : scriptSources) {
     System.out.println("src:" + element.absUrl("src"));
}