How to visit a page several times in a row with PhantomJS?

为君一笑 提交于 2019-12-08 07:16:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!