【问题标题】:phantomjs - execute a Javascript function after page load and then output new changesphantomjs - 页面加载后执行 Javascript 函数,然后输出新的更改
【发布时间】:2016-02-18 04:40:28
【问题描述】:

我使用 phantomjs 2.1.1,但有些事情让我很困扰。 这是我用来抓取 url 的一段代码,网站的 html 被写入 output.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 函数 page(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 后网站内容发生的新更改,在这种情况下,网站将调用下一页(使用 ajax)在第(2)页之后;方法调用。

提前致谢!

【问题讨论】:

    标签: javascript php web-scraping phantomjs casperjs


    【解决方案1】:

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

    代码:

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

    【讨论】:

      【解决方案2】:

      我最近发布了一个让 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.
      

      【讨论】:

        猜你喜欢
        • 2021-06-02
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 2014-11-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多