jsoup深度遍历html中所有的子链接

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

import java.io.IOException;
import java.util.*;

public class testDOM  {


    public static void main(String[] args) {
        String url = "https://mirrors.edge.kernel.org/pub/software/libs/glibc/hjl/";
        getAllUrl(url);
    }



    public static List getAllUrl(String url){

        List list = new ArrayList();
       try {
           Document doc = (Document) Jsoup.connect(url).get();
           Elements links = ((Element) doc).select("a[href]");
           for (Element link : links) {
               String linkHref = link.attr("abs:href");
               String linkText = link.text();
               String test = linkHref+"............................"+linkText;
               System.out.println(test);
               list.add(linkHref);
               if(!linkText.endsWith("../")&&linkText.endsWith("/")){
                    getAllUrl(linkHref);
               }
           }

       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
        return list;
   }




}

改进版如下:

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

import java.io.IOException;
import java.util.*;

public class testDOM  {
    private  static List   list;

    public static void main(String[] args) {
        list = new ArrayList();
        String url = "https://mirrors.edge.kernel.org/pub/software/libs/glibc/hjl/";
         List list2 = getAllUrl(url);
        for (int i = 0; i 

所需jar包链接:密码: 9ir5

你可能感兴趣的:(Java)