Using Multiple page.open in Single Script

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

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;

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

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

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

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.

回答1:

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


回答2:

I've tried the accepted answer suggestions, but it doesn't work (at least not for v2.1.1).

To be accurate the accepted answer worked some of the time, but I still experienced sporadic failed page.open() calls, about 90% of the time on specific data sets.

The simplest answer I found is to instantiate a new page module for each url.

// first page var urlA = "http://first/url" var pageA = require('webpage').create()  pageA.open(urlA, function(status){     if (status){         setTimeout(openPageB, 100) // open second page call     } else{         phantom.exit(1)     } })  // second page var urlB = "http://second/url" var pageB = require('webpage').create()  function openPageB(){     pageB.open(urlB, function(){         // ...          // ...     }) }

The following from the page module api documentation on the close method says:

close() {void}

Close the page and releases the memory heap associated with it. Do not use the page instance after calling this.

Due to some technical limitations, the web page object might not be completely garbage collected. This is often encountered when the same object is used over and over again. Calling this function may stop the increasing heap allocation.

Basically after I tested the close() method I decided using the same web page instance for different open() calls is too unreliable and it needed to be said.



回答3:

You can use recursion:

var page = require('webpage').create();  // the urls to navigate to var urls = [     'http://phantomjs.org/',     'https://twitter.com/sidanmor',     'https://github.com/sidanmor' ];  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!