Using Multiple page.open in Single Script

前端 未结 4 1626
感动是毒
感动是毒 2020-11-27 04:36

My goal is to execute PhantomJS by using:

// adding $op and $er for debugging purposes
exec(\'phantomjs script.js\', $op, $er);
print_r($op);
echo $er;
         


        
4条回答
  •  星月不相逢
    2020-11-27 04:37

    The problem is that the second page.open is being invoked before the first one finishes, which can cause multiple problems. You want logic roughly like the following (assuming the filenames are given as command line arguments):

    function handle_page(file){
        page.open(file,function(){
            ...
            page.evaluate(function(){
                ...do stuff...
            });
            page.render(...);
            setTimeout(next_page,100);
        });
    }
    function next_page(){
        var file=args.shift();
        if(!file){phantom.exit(0);}
        handle_page(file);
    }
    next_page();
    

    Right, it's recursive. This ensures that the processing of the function passed to page.open finishes, with a little 100ms grace period, before you go to the next file.

    By the way, you don't need to keep repeating

    page = require('webpage').create();
    

提交回复
热议问题