为什么phantomjs代码不通过数组? [英] why phantomjs code doesn't go through array?

查看:120
本文介绍了为什么phantomjs代码不通过数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码后,他使用第一页的源代码保存了无数个文件( http://site1.com ),为什么他不通过其他链接而不停止?

After running this code he save an infinite number of files with the source code of the first page("http://site1.com"), why he doesn't go through other links and doesn't stop ?

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.content 在外部上下文中可用:

page.evaluate() is the sandboxed page context in PhantomJS. It doesn't have access to any variable defined outside. So you cannot reference fs or page inside of it and you don't need to, because page.content is available in the outer context:

page.open(file,function(){
    fs.write(i + '.html', page.content, 'w');
    setTimeout(next_page,100);
});

剩下的代码看起来很好。

The remaining code looks fine.

这篇关于为什么phantomjs代码不通过数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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