【问题标题】:phantomjs page.open() doesn't seem to existphantomjs page.open() 似乎不存在
【发布时间】:2013-02-11 17:28:21
【问题描述】:

我正在尝试使用 phantomjs 获取页面内容。在官方网站(例如:https://github.com/ariya/phantomjs/blob/master/examples/imagebin.js)的许多示例中,使用了 page.open() 函数。
在我的脚本中,虽然它似乎不起作用。我使用反射来查看页面对象的所有定义方法:

  for ( var prop in page) {
      if (typeof page[prop] == 'function') {
          log("method in page: " + prop);
      }
  }

并且 open() 方法没有出现。 (close()、render() 等...确实出现了)
当我尝试执行脚本时:

// include plugins
var system = require('system');
var fileSystem = require('fs');
var page = require('webpage').create();

// global errorhandler
phantom.onError = function(msg, trace) {
  console.log("ERROR!!!!! \n" + msg);
  phantom.exit(1);
};

// read json input and remove single outer quotes if set
var jsonin = system.args[1];
if (jsonin.charAt(0) == "'") {
  jsonin = jsonin.substr(1, jsonin.length - 2);
}
// make object of json
var data = eval('(' + jsonin + ')');
// optional url
var url = system.args[2];
// transfer file
var dest = system.args[3];

console.log("systemargs[1]: data -> " + data);
console.log("systemargs[2]: url -> " + url);
console.log("systemargs[3]: dest -> " + dest);

openRoot();

/*
 * open site
 */
function openRoot() {    
  page.onConsoleMessage = function(msg) {
    console.log('INNER ' + msg);
  };

  page.open(url, function(status) {
    if (status === "success") {
      if (loadCount == 0) { // only initial open
        console.log("opened successfully.");
        page.injectJs("./jquery-1.8.3.min.js");
      } else {
        console.log("page open error.");
        console.log('skip refresh ' + loadCount);
      }

    } else {
      console.log("error opening: " + status);
    }
  });
}
phantom.exit(0);

它不执行打开功能。日志不显示 open() 方法内的任何消息。

任何关于我可能做错的建议将不胜感激。如果需要其他信息,请告诉我。
问候,
亚历克斯

编辑:
线

console.log(typeof (page.open));

输出:function 这不是我所期望的,因为我之前写入日志的方法列表,其中 open 不存在。嗯。

【问题讨论】:

    标签: java phantomjs


    【解决方案1】:

    经过数小时毫无意义的搜索后,我发现了错误。愚蠢的我。在脚本的结尾,我不应该打电话给phantom.exit()

    工作代码包括一个检查对象的间隔,在我的例子中是contentcontent.isFinished 的成员。如果我将此设置为 true,则将调用 phantom.exit()
    我的错,绝对是我的错。
    工作代码:

    var url = system.args[2];
    // transfer file
    var dest = system.args[3];
    
    content = new Object();
    content.isFinished = false;
    
    console.log("systemargs[1]: data -> " + data);
    console.log("systemargs[2]: url -> " + url);
    console.log("systemargs[3]: dest -> " + dest);
    
    openRoot();
    
    /*
     * open site
     */
    function openRoot() {    
      page.onConsoleMessage = function(msg) {
        console.log('INNER ' + msg);
      };
    
      page.open(url, function(status) {
        if (status === "success") {
          if (loadCount == 0) { // only initial open
            console.log("opened successfully.");
            page.injectJs("./jquery-1.8.3.min.js");
    
            // do stuff
            content.isFinished = true;
          } else {
            console.log("page open error.");
            console.log('skip refresh ' + loadCount);
            content.isFinished = true
          }
    
        } else {
          console.log("error opening: " + status);
        }
      });
    }
    
    /*
     * wait for completion
     */
    var interval = setInterval(function() {
      if (content.isFinished) {
        page.close();
    
        f = fileSystem.open(dest, "w");
        f.writeLine(out);
        f.close();
    
        // exit phantom
        phantom.exit();
      } else {
        console.log('not finished - wait.');
      }
    }, 5000);
    

    问候,
    亚历克斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多