Using Multiple page.open in Single Script

前端 未结 4 1624
感动是毒
感动是毒 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:40

    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.

提交回复
热议问题