运行这段代码后,他保存了无数个带有第一页源代码的文件(“http://site1.com”),为什么他不通过其他链接并且不停歇?
var args = ["http://site1.com", "http://site2.com", "http://site3.com"];
var fs = require('fs');
var i = 0;
function handle_page(file){
page.open(file,function(){
page.evaluate(function(){
fs.write(i + '.html', page.content, 'w');
});
setTimeout(next_page,100);
});
}
function next_page(){
var file = args.shift();
if(!file){ phantom.exit(0); }
i++
handle_page(file);
}
next_page();
最佳答案
page.evaluate()
是 PhantomJS 中的沙盒页面上下文。它无权访问外部定义的任何变量。因此,您无法在其中引用 fs
或 page
,也不需要这样做,因为 page.content
在外部上下文中可用:
page.open(file,function(){
fs.write(i + '.html', page.content, 'w');
setTimeout(next_page,100);
});
剩下的代码看起来不错。
关于javascript - 为什么 phantomjs 代码不通过数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29317439/