在我的一项任务中,我必须使用纯粹的JavaScript来制作网络抓取工具。这意味着,输入将是一个URL,输出将是从该页面开始的所有链接的树。 我使用了使用YQL的插件https://github.com/padolsey/jQuery-Plugins/tree/master/cross-domain-ajax/,它可以用来获取网站上的所有链接。 (就像我在谷歌的主页上所做的那样,http://deepakpathak.in/langoor/linkfinder.html)。但是我在制作树状结构时遇到了麻烦。 有没有其他更简单的方法来制作给定网站的链接树,以及Javascript中的链接树?
答案 0 :(得分:1)
如果输出是某种网站上的树列表组件,或者您想将其放入数据库中,那么您没有提到要使用哪种树?
但是,您可以使用普通的旧javascript对象和数组来创建tree(查看树数据结构上的任何注释,看看它们是如何工作的,有很多方法可以表示它们)。为了帮助您入门,基本树可能如下所示(使用createNode
函数):
var createNode = function(content) {
return {
'nodes': [], // children are put here
'content': content // puts the content here
};
};
var treeroot = createNode();
// create the root node of the tree
// content is undefined
treeroot.nodes.push(createNode(myTreeNode));
// accesses the nodes array and pushes a new node into the root node
// content in that node will be whatever "myTreeNode" is
你必须自己编写遍历算法,因为javascript没有任何处理树的函数。或者使用DOM本身创建树(因为它是树数据结构)。