Java 如何使用jsoup获取href

Java 如何使用jsoup获取href,java,recursion,jsoup,Java,Recursion,Jsoup,我有一些网址。我想从指向的html url获取所有href,从所有获取的href获取所有href(递归)。重点是我想设置“递归”的深度 例如,如果depth=1,我只需要HTML中的href。如果depth=2,我需要来自HTML的href(假设为list1)和来自list1的每个href的href,依此类推 以下是我在使用jsoup时得到的信息: import org.jsoup.*; import org.jsoup.nodes.Document; import org.jsoup.node

我有一些网址。我想从指向的html url获取所有href,从所有获取的href获取所有href(递归)。重点是我想设置“递归”的深度 例如,如果depth=1,我只需要HTML中的href。如果depth=2,我需要来自HTML的href(假设为list1)和来自list1的每个href的href,依此类推

以下是我在使用jsoup时得到的信息:

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class Parser {
    private final static String FILE_PATH = "src/main/resources/href.txt";
    private List<String> result;

    private int currentDepth;
    private int maxDepth;

    public Parser(int maxDepth) {
        result = new ArrayList<String>();
        this.maxDepth = maxDepth;
    }

    public void parseURL(String url) throws IOException {
        url = url.toLowerCase();
        if (!result.contains(url)) {
            Connection connection = Jsoup.connect(url);
            Document document = connection.get();
            Elements links = document.select("a[href]");
            for (Element link : links) {
                String href = link.attr("href");
                result.add(href);
                parseURL(link.absUrl("href"));
                currentDepth++;
                if (currentDepth == maxDepth)
                    return;
            }
        }
    }
}
import org.jsoup.*;
导入org.jsoup.nodes.Document;
导入org.jsoup.nodes.Element;
导入org.jsoup.select.Elements;
导入java.io.File;
导入java.io.IOException;
导入java.io.PrintWriter;
导入java.util.ArrayList;
导入java.util.List;
公共类解析器{
私有最终静态字符串文件_PATH=“src/main/resources/href.txt”;
私有列表结果;
私家侦探深度;
私有整数最大深度;
公共解析器(int-maxDepth){
结果=新的ArrayList();
this.maxDepth=maxDepth;
}
公共void parseURL(字符串url)引发IOException{
url=url.toLowerCase();
如果(!result.contains(url)){
Connection=Jsoup.connect(url);
Document=connection.get();
元素链接=文档。选择(“a[href]”;
用于(元素链接:链接){
String href=link.attr(“href”);
结果.添加(href);
parseURL(link.absUrl(“href”);
currentDepth++;
如果(currentDepth==maxDepth)
回来
}
}
}
}

如何修复递归条件以使其正确?

我认为在调用递归函数之前,应该先检查深度

if (currentDepth >= maxDepth){
    // do nothing
}else{
    parseURL(...)
}
您可以在代码中尝试此操作,您可以从方法
getElementsByAttribute(String属性)
中获取具有指定属性的所有元素

  public void parseURL(String url) throws IOException {
    url = url.toLowerCase();
    if (!result.contains(url)) {
        Connection connection = Jsoup.connect(url);
        Document document = connection.get();
        Elements links = document.getElementsByAttribute("href");
       // Elements links = document.select("a[href]");
        for (Element link : links) {
            String href = link.attr("href");
            result.add(href);
            parseURL(link.absUrl("href"));
            currentDepth++;
            if (currentDepth == maxDepth)
                return;
        }
    }
}