在单个脚本中使用多个page.open [英] Using Multiple page.open in Single Script

查看:142
本文介绍了在单个脚本中使用多个page.open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用以下命令执行PhantomJS:

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;

然后在 script.js 里面,我计划使用多个 page.open()来捕获不同页面的屏幕截图,例如:

And then inside script.js, I plan to use multiple page.open() to capture screenshots of different pages such as:

var url = 'some dynamic url goes here';
page = require('webpage').create();
page.open(url, function (status) {
    console.log('opening page 1');  
    page.render('./slide1.png');            
});

page = require('webpage').create();
page.open(url, function (status) {
    console.log('opening page 2');  
    page.render('./slide2.png');        
});

page = require('webpage').create();
page.open(url, function (status) {
    console.log('opening page 3');  
    page.render('./slide3.png');        
    phantom.exit(); //<-- Exiting phantomJS only after opening all 3 pages
});

在运行 exec 时,我得到以下内容页面输出:

On running exec, I get the following output on page:

Array ( [0] => opening page 3 ) 0

结果我只得到第3页的屏幕截图。我不确定为什么PhantomJS会跳过第一和第二个代码块(从丢失的 console.log()消息中可以看出,这些消息应该从第1和第2个输出块)并且只执行第三块代码。

As a result I only get the screenshot of the 3rd page. I'm not sure why PhantomJS is skipping the first and second blocks of code (evident from the missing console.log() messages that were supposed to be output from 1st and 2nd block) and only executing the third block of code.

推荐答案

问题是第二个页面。在第一个完成之前调用open ,这可能会导致多个问题。您希望逻辑大致如下(假设文件名作为命令行参数给出):

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

对,它是递归的。这样可以确保在转到下一个文件之前,传递给 page.open 的函数的处理完成,只需要100ms的宽限期。

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

这篇关于在单个脚本中使用多个page.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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