why phantomjs code doesn't go through array?

巧了我就是萌 提交于 2019-12-02 08:10:51

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/29317439/why-phantomjs-code-doesnt-go-through-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!