【问题标题】:Getting sub links of a URL using jsoup使用 jsoup 获取 URL 的子链接
【发布时间】:2017-08-20 02:21:38
【问题描述】:

考虑一个 URL www.example.com 它可能有很多链接,有些可能是内部的,有些可能是外部的。我想获得所有子链接的列表,甚至不是子子链接,而只是子链接。 例如,如果有四个链接如下

1)www.example.com/images/main
2)www.example.com/data
3)www.example.com/users
4)www.example.com/admin/data

那么在这四个中只有2和3是有用的,因为它们是子链接而不是子子等等链接。有没有办法通过j-soup实现它..如果这不能通过j-soup 然后可以向我介绍一些其他的 java API。 另请注意,它应该是最初发送的父 URL 的链接(即 www.example.com)

【问题讨论】:

    标签: java jsoup


    【解决方案1】:

    如果我能理解子链接可以包含一个斜杠,您可以尝试计算斜杠的数量,例如:

    List<String> list = new ArrayList<>();
    list.add("www.example.com/images/main");
    list.add("www.example.com/data");
    list.add("www.example.com/users");
    list.add("www.example.com/admin/data");
    

    for(String link : list){
        if((link.length() - link.replaceAll("[/]", "").length()) == 1){
            System.out.println(link);
        }
    }
    

    link.length():统计字符数
    link.replaceAll("[/]", "").length():统计斜线数

    如果差值等于一,则右链接否则不。


    编辑

    我将如何扫描整个网站的子链接?

    robots.txt 文件或Robots exclusion standard 的答案,因此它定义了网站的所有子链接,例如https://stackoverflow.com/robots.txt,所以想法是,要阅读此文件,您可以从该网站提取子链接,这是一段可以帮助您的代码:

    public static void main(String[] args) throws Exception {
    
        //Your web site
        String website = "http://stackoverflow.com";
        //We will read the URL https://stackoverflow.com/robots.txt
        URL url = new URL(website + "/robots.txt");
    
        //List of your sub-links
        List<String> list;
    
        //Read the file with BufferedReader
        try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
            String subLink;
            list = new ArrayList<>();
    
            //Loop throw your file
            while ((subLink = in.readLine()) != null) {
    
                //Check if the sub-link is match with this regex, if yes then add it to your list
                if (subLink.matches("Disallow: \\/\\w+\\/")) {
                    list.add(website + "/" + subLink.replace("Disallow: /", ""));
                }else{
                    System.out.println("not match");
                }
            }
        }
    
        //Print your result
        System.out.println(list);
    }
    

    这会告诉你:

    [https://stackoverflow.com/posts/, https://stackoverflow.com/posts?, https://stackoverflow.com/search/, https://stackoverflow.com/search?, https://stackoverflow.com/feeds/, https://stackoverflow.com/feeds?, https://stackoverflow.com/unanswered/, https://stackoverflow.com/unanswered?, https://stackoverflow.com/u/, https://stackoverflow.com/messages/, https://stackoverflow.com/ajax/, https://stackoverflow.com/plugins/]

    这是Demo about the regex that i use

    希望对你有帮助。

    【讨论】:

    • 但是我将如何扫描整个网站的子链接
    • 在我获得网站中的所有内部链接后,您的实施将起作用
    • 检查我的编辑@javafan 的想法是阅读 robots.txt 它包含网站的所有信息,因此您可以从那里提取子链接
    • 我能知道你为什么删除了@javafan 接受的答案吗?
    • 对不起,我不知道我们不能接受两个答案
    【解决方案2】:

    要扫描网页上的链接,您可以使用 JSoup 库。

    import java.io.IOException;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    class read_data {
    
        public static void main(String[] args) {
            try {
                Document doc = Jsoup.connect("**your_url**").get();
                Elements links = doc.select("a");
                List<String> list = new ArrayList<>();
                for (Element link : links) {
                    list.add(link.attr("abs:href"));
                }
            } catch (IOException ex) {
    
            }
        }
    }
    

    list 可以按照上一个答案中的建议使用。


    下面给出了读取网站上所有链接的代码。我用http://stackoverflow.com/ 来说明。我建议你在抓取它的网站之前通过公司的terms of use

    import java.io.IOException;
    import java.util.HashSet;
    import java.util.Set;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.select.Elements;
    
    public class readAllLinks {
    
        public static Set<String> uniqueURL = new HashSet<String>();
        public static String my_site;
    
        public static void main(String[] args) {
    
            readAllLinks obj = new readAllLinks();
            my_site = "stackoverflow.com";
            obj.get_links("http://stackoverflow.com/");
        }
    
        private void get_links(String url) {
            try {
                Document doc = Jsoup.connect(url).get();
                Elements links = doc.select("a");
                links.stream().map((link) -> link.attr("abs:href")).forEachOrdered((this_url) -> {
                    boolean add = uniqueURL.add(this_url);
                    if (add && this_url.contains(my_site)) {
                        System.out.println(this_url);
                        get_links(this_url);
                    }
                });
    
            } catch (IOException ex) {
    
            }
    
        }
    }
    

    您将获得uniqueURL 字段中所有链接的列表。

    【讨论】:

    • 感谢您的帮助,但让我告诉您,我不想简单地在网页上获取链接,我想获取整个网站的链接。
    • 你可以看到this。如果这对您不起作用,请告诉我。
    • 我只是在几个网站上试过这个:your_url/sitemap.xml。让我知道它是否有效
    • 我尝试了堆栈溢出,但它不起作用。但是当我在我的网站上尝试它时它起作用了
    • 我已经编辑了答案,希望对您有所帮助,如果对您有帮助,请接受。
    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多