How to get all the URLs in a web site using JavaScript?

前端 未结 4 2133
借酒劲吻你
借酒劲吻你 2021-01-02 17:50

Any one knows a way to get all the URLs in a website using JavaScript?

I only need the links starting with the same domain name.no need to consider other links.

4条回答
  •  猫巷女王i
    2021-01-02 18:19

    Well this will get all the same-host links on the page:

    var urls = [];
    for(var i = document.links.length; i --> 0;)
        if(document.links[i].hostname === location.hostname)
            urls.push(document.links[i].href);
    

    If by site you mean you want to recursively get the links inside linked pages, that's a bit trickier. You'd have to download each link into a new document (for example in an