问题
Is it possible to open page, do my things and close it. And after a timeout visit it once again to see the content changes (page has many js functions). Trying to open the page twice in a row makes PhantomJS behave unpredictable. What is the solution then?
回答1:
Most the execution in PhantomJS is asynchronous, so you have to open a page only after the first page load was completed:
page.open(url, function(){
setTimeout(function(){
// do something
page.open(url, function(){
setTimeout(function(){
// do something
phantom.exit();
}, 5000); // 5 seconds
});
}, 5000); // 5 seconds
});
or even better using recursion:
var i = 0;
function run(){
if (i > 100) { // stop execution at some point
phantom.exit();
}
page.open(url, function(){
// do what you have to do
setTimeout(run, 5000); // run again in 5 seconds
});
}
run();
来源:https://stackoverflow.com/questions/28525123/how-to-visit-a-page-several-times-in-a-row-with-phantomjs