我想以给定的格式将 ExecutorService 应用于来自 url 的递归读取路径,我很难尝试调用递归 getRecursive(element.attr("abs:href"), level);
public class NewClass {
static String levels[] = {"div.col-md-9 li a", "div#sidebar ul li a"};
private void getRecursive(String href,int level) {
if (level > levels.length - 1) {
return;
}
Document doc;
try {
doc = Jsoup.connect(href).get();
Elements elements = doc.select(levels[level]);
level++;
ExecutorService executor = Executors.newFixedThreadPool(5);
for (final Element element : elements) {
executor.execute(new Runnable() {
@Override
public void run() {
if (!element.attr("href").isEmpty()) {
String links = "";
links += element.attr("abs:href") + "\n";
System.out.println(links);
getRecursive(element.attr("abs:href"), level);
}
}
});
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
new NewClass().getRecursive("http://www.java2s.com/", 0);
}
}