如何使用JavaScript获取网站中的所有网址?

编程入门 行业动态 更新时间:2024-10-28 18:23:20
本文介绍了如何使用JavaScript获取网站中的所有网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任何人都知道使用JavaScript获取网站中所有网址的方法吗?

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.

推荐答案

这将获得页面上所有相同的主机链接:

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

如果通过 site ,则表示您希望以递归方式获取链接内的链接页面,这有点棘手。您必须将每个链接下载到新文档中(例如,在< iframe> 中),并且 onload 检查iframe自己的文档,以获取更多链接以添加到要获取的列表。您需要查找已经抓取的URL,以避免两次获取同一文档。它可能不会很快。

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 <iframe>), and the onload check the iframe's own document for more links to add to the list to fetch. You'd need to keep a lookup of what URLs you'd already spidered to avoid fetching the same document twice. It probably wouldn't be very fast.

更多推荐

如何使用JavaScript获取网站中的所有网址?

本文发布于:2023-11-28 15:59:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   网址   网站   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!