获取网站的所有href属性 [英] get all the href attributes of a web site

查看:136
本文介绍了获取网站的所有href属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我一种使用javascript获取网站中所有href属性(链接)的方法吗?如果你能给我一个代码示例,我将非常感激。

can anybody tell me a way to get all the href attributes(links) in a web site using javascript?if you could give me a code example, i will be most thankful.

推荐答案

您可以使用 document.links 获取锚点,然后循环抓取 href ,如下所示:

You can use document.links to get the anchors, then just loop through grabbing the href, like this:

var arr = [], l = document.links;
for(var i=0; i<l.length; i++) {
  arr.push(l[i].href);
}
//arr is now an array of all the href attributes from the anchors in the page

你可以在这里测试一下,你可以在 .push() 如果你愿意,可以调用数组,但这是抓取链接和循环的概念。

You can test it out here, you could filter it more before the .push() call on the array if you wanted, but that's the concept for grabbing the links and looping through.

这篇关于获取网站的所有href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆