phantomjs - 在页面加载后执行Javascript函数,然后输出新的更改


phantomjs - execute a Javascript function after page load and then output new changes

我使用phantomjs 2.1.1,有些事情困扰着我。这是我用来抓取网址的代码段,网站的html被写入输出.html文件

page = require('webpage').create();
    page.open(url, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
            phantom.exit();
        } else {
            window.setTimeout(function () {
                var content = page.content;
                fs.write("output.html", content, 'w');
            }, 40000); //40 seconds timeout
        }
    });

现在,我也需要刮掉它的分页。接下来的页面由 javascript 函数页面(2) 加载;或页面(3);我试图使用

 var pageinationOutput = page.evaluate(function (s) {
    page(2);
 });
 console.log(pageinationOutput); // I need the output made by the  `page(2);` call.
        page = require('webpage').create();
            page.open(url, function (status) {
                if (status !== 'success') {
                    console.log('Unable to load the address!');
                    phantom.exit();
                } else {
                    window.setTimeout(function () {
                        var content = page.content;
                        fs.write("output.html", content, 'w');
                    }, 40000); //40 seconds timeout
                }
            });

但我没有得到任何输出。如何在页面加载完成后执行 JavaScript 函数,并在 javascript exec 之后获取网站内容发生的新更改,在这种情况下,网站将在 page(2) 之后调用下一页(使用 ajax);方法调用。

提前感谢!

我自己找到了解决方案,但我不确定这是否是完美的方法。

法典:

page.open(url, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            var content = page.content;
            fs.write("output.html", content, 'w');
            page.evaluate(function (cb) {
                window.page(2);
            });
            var waiter = window.setInterval(function () {
                var nextPageContent = page.evaluate(function (cb) {
                    return document.documentElement.outerHTML;
                });
                if (nextPageContent !== false) {
                    window.clearInterval(waiter);
                    fs.write("output-2.html", content, 'w');
                }
            }, 40000);//40 seconds timeout  
        }, 40000);//40 seconds timeout  
    }
});

我最近发布了一个项目,该项目允许PHP访问浏览器。在这里获取:https://github.com/merlinthemagic/MTS。它也是引擎盖下的PhantomJS。

如果您提供了URL,我可以做一个工作示例。我需要知道你如何确定最后一页。在示例中,我只是将其设置为 10。我还需要知道页面按钮是否具有id属性,如果它们没有问题,我们会找到另一种触发它们的方法。但是对于此示例,我假设他们这样做,并且为了使它简单,ids将被page_2,page_3....

下载并设置后,您只需使用以下代码:

$myUrl          = "http://www.example.com";
$windowObj      = 'MTS'Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);
//now you can either retrieve the DOM for each page:
$doms = array();
//get the initial page DOM
$doms[]  = $windowObj->getDom();
$pageID   = "page_";
$lastPage = 10;
for ($i = 2; $i <= $lastPage; $i++) {
   $windowObj->mouseEventOnElement("[id=".$pageID. $i . "]", 'leftclick');
   $doms[]  = $windowObj->getDom();
}
//$doms now hold all the pages, so you can parse them.