phantomjs 可以与 node.js 一起使用吗?

IT技术 javascript jquery node.js coffeescript phantomjs
2021-02-22 00:34:17

我想在我的 node.js 脚本中使用 phantomjs。有一个phantomjs-node库..但不幸的是,作者使用这个奇怪的咖啡脚本代码来解释他在做什么:

phantom = require 'phantom'

phantom.create (ph) ->
  ph.createPage (page) ->
    page.open "http://www.google.com", (status) ->
      console.log "opened google? ", status
      page.evaluate (-> document.title), (result) ->
        console.log 'Page title is ' + result
        ph.exit()

现在,如果我直接在 javascript 中使用 phantomjs,它看起来像这样

var page = require('webpage').create();
page.open(url, function (status) {
    var title = page.evaluate(function () {
        return document.title;
    });
    console.log('Page title is ' + title);
});

所以基本上我试图在普通的javascript中编写相当于上面的第一个代码片段(通过阅读咖啡脚本文档..这就是我所做的:

// file name: phantomTest.js

var phantom = require('phantom');

phantom.create(function(ph) {
    ph.createPage(function(page) {
        page.open('http://www.google.com', function(status) {
            console.log('opened google?', status);
            var title = page.evaluate(function() {
                return document.title;
            });
            console.log('page title is ' + title);              
        });
    });
    ph.exit();
});

不幸的是它不起作用!如果我跑

node phantomTest.js

在外壳上,没有任何react.. 没有任何返回并且该过程不会停止.. 有什么想法吗?

更新:

我刚刚在 phantomjs常见问题解答中读到了这个

问:为什么 PhantomJS 没有写成 Node.js module?

答:简答:“没有人可以侍奉两个主人。”

更长的解释如下。

到目前为止,这样做在技术上非常具有挑战性。

每个 Node.js module本质上都是 Node.js 核心的“奴隶”,即“主人”。在当前状态下,PhantomJS(及其包含的 WebKit)需要完全控制(以同步方式)一切:事件循环、网络堆栈和 JavaScript 执行。

如果目的只是直接从 Node.js 中运行的脚本中使用 PhantomJS,那么可以通过启动 PhantomJS 进程并与之交互来实现这种“松散绑定”。

嗯..这可能与它有关吗?但那样整个图书馆就没有意义了!

更新2:

我在网上找到了这段代码,它做同样的事情:

var phantom = require('phantom');
phantom.create(function(ph) {
  return ph.createPage(function(page) {
    return page.open("http://www.google.com", function(status) {
      console.log("opened google? ", status);
      return page.evaluate((function() {
        return document.title;
      }), function(result) {
        console.log('Page title is ' + result);
        return ph.exit();
      });
    });
  });
});

不幸的是,这也不起作用……同样的结果!

6个回答

phantomjs-node 不是 phantomjs 官方支持的 npm 包。相反,它通过创建一个使用 websockets 作为节点和幻像之间的 IPC 通道的 Web 服务器,在节点和幻像之间实现了一个“令人作呕的聪明桥梁”。我不是在编造这个

因此,我们通过启动 ExpressJS 的一个实例,在子进程中打开 Phantom,并将其指向一个将 socket.io 消息转换为 alert() 调用的特殊网页来与 PhantomJS 进行通信。那些 alert() 调用由 Phantom 接收,然后就可以了!

因此,如果 phantomjs-node 工作、不工作、无声地失败或严重失败,我不会感到惊讶。我也不希望 phantomjs-node 的作者以外的任何人能够对 phantomjs-node 进行故障排除。

你原来问题的答案是来自phantomjs faq的答案:不会。Phantom和node有不可调和的差异。两者都希望完全控制基本的低级功能,例如事件循环、网络堆栈和 JS 执行,因此它们无法在同一个进程中进行协作。

哇,真讨厌!那么下一个问题是:使用 jquery 抓取动态页面的最佳方法是什么?
2021-04-21 00:34:17
为什么不可能?我想我找到了我的答案..一如既往..我从不可能的艰难路线开始,只是为了找到一个更简单的解决方案哈哈..我会给你正确答案奖..
2021-05-06 00:34:17
如果您在 jQuery 中使用完整的 DOM 和 JS 运行时实现,这是可能的,但我认为这不是一个可行的解决方案。
2021-05-17 00:34:17
它是!它现在对我来说就像一个魅力..只需将原始 html 转储到 Dom 结构中.. 使用 jquery 魔法遍历它.. 每个人都很高兴!看看我对上述问题的愤怒更新;)
2021-05-18 00:34:17
@abood 我认为这是不可能的。你在这里的目标是什么?
2021-05-19 00:34:17

你也可以试试phridge你的例子应该是这样写的:

var phantom;

// spawn a new PhantomJS process
phridge.spawn()
    .then(function (ph) {
        phantom = ph;
        return phantom.openPage("http://www.google.com");
    })
    .then(function (page) {
        return page.run(function () {
            // this function runs inside PhantomJS with this bound to a webpage instance
            return this.title;
        });
    })
    .then(function (title) {
        console.log('Page title is ' + title);
        // terminates the process cleanly
        phantom.dispose();
    });

我现在是phantom-nodepackage的新维护者它不再使用咖啡脚本了。你可以做类似的事情

var phantom = require('phantom');

phantom.create().then(function(ph) {
  ph.createPage().then(function(page) {
    page.open('https://stackoverflow.com/').then(function(status) {
      console.log(status);
      page.property('content').then(function(content) {
        console.log(content);
        page.close();
        ph.exit();
      });
    });
  });
});

新版本的速度和弹性要快得多。它也不再使用 websockets。

将您的代码更改为此,它将起作用:

 var phantom = require('phantom');
 phantom.create(function(ph) {
   ph.createPage(function(page) {
     page.open("http://www.google.com", function(status) {
       console.log("opened google? ", status);
       page.evaluate((function() {
         return document.title;
       }), function(result) {
         console.log('Page title is ' + result);
         ph.exit();
       });
     });
   });
 });

你可以像我一样放弃 PhantomJS,因为这些包装器不能很好地工作实在是太痛苦了,而使用Zombie.js,它也很受欢迎。