Phantomjs不在page.evaluate函数中执行函数 [英] Phantomjs does not execute function in page.evaluate function

查看:122
本文介绍了Phantomjs不在page.evaluate函数中执行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PhantomJS节点模块抓取Facebook页面( https://github.com/sgentle / phantomjs-node ),但是当我尝试评估页面时,它不会评估我传递给它的函数。在独立脚本中执行它并使用Node解释器运行.. Express.js应用程序中的相同代码不起作用。

I'm scraping a Facebook page with the PhantomJS node module (https://github.com/sgentle/phantomjs-node), but when I try evaluating the page, it does not evaluate the function I pass to it. Executing it in a standalone script and running it with the Node interpreter works.. The same code in an Express.js app does not work.

这是我的代码

facebookScraper.prototype.scrapeFeed = function (url, cb) {
    f = ':scrapeFeed:';

    var evaluator = function (s) {
        var posts = [];

        for (var i = 0; i < FEED_ITEMS; i++) {
            log.info(__filename+f+' iterating step ' + i);
            log.info(__filename+f+util.inspect(document, false, null));
        }

        return {
            news: posts
        };
    }

    phantom.create(function (ph) {
        ph.createPage(function (page) {
            log.fine(__filename+f+' opening url ' + url);
            page.open(url, function (status) {
                log.fine(__filename+f+' opened site? ' + status);
                setTimeout(function() {
                    page.evaluate(evaluator, function (result) {
                        log.info(__filename+f+'Scraped feed: ' + util.inspect(result, false, null));
                        cb(result, ph);
                    });
                }, 5000);
            });
        });
    });
};

我得到的输出:

{"level":"fine","message":"PATH/fb_regular.js:scrapeFeed: opening url <URL> ","timestamp":"2012-09-23T18:35:10.151Z"}
{"level":"fine","message":"PATH/fb_regular.js:scrapeFeed: opened site? success","timestamp":"2012-09-23T18:35:12.682Z"}
{"level":"info","message":"PATH/fb_regular.js:scrapeFeed: Scraped feed: null","timestamp":"2012-09-23T18:35:12.687Z"}

因此,如您所见,它调用幻像回调函数(评估中的第二个参数)函数)有一个null参数,但是它没有执行第一个参数(我的评估函数,它打印迭代步骤X)。

So, as you see, it calls the phantom callback function (second parameter in the evaluate function) with a null argument, but it doesn't execute the first parameter (my evaluator function, which prints iterating step X).

任何人都知道问题是什么?

Anyone knows what the problem is?

推荐答案

我不确定你使用的是什么版本的PhantomJS,但是对于1.6+版本的文档记录evaluate script将结果记录在包含的页面中。它不会登录到您的控制台。为此,您必须将日志记录绑定到onConsoleMessage事件的页面:

I'm unsure as to what version of PhantomJS you are using, but as for the documentation of versions 1.6+ logging inside evaluated script will log the result in the contained page. It will not log into your console. To get that you would have to bind logging to the pages onConsoleMessage event:

  page.onConsoleMessage = function (msg) { console.log(msg); };

至于结果不可用:page.evaluate函数接受这样的参数 - 第一个是要执行的函数,其余的作为输入传递给该函数。结果直接返回:

As for the result not being available: The page.evaluate function takes arguments like so - first one is a function to be executed and the rest are passed as input to that function. The result is returned directly:

 var title = page.evaluate(function (s) {
    return document.querySelector(s).innerText;
 }, 'title');
 console.log(title);

这篇关于Phantomjs不在page.evaluate函数中执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆