我正在评估phantom.js和 zombie.js 。我预计的权衡是幻影具有更广泛的文档支持(因为它使用真正的渲染器),而 zombie 速度更快(因为不使用渲染引擎)。然而,在我所做的测试中, zombie 似乎慢得多。这有道理吗?
我在想也许 zombie 在访问()返回之前等待整个页面加载(包括运行所有脚本和加载CSS),而幻影在启动()(我使用casperjs)之后立即返回,允许我继续而无需等待整页。
Phantom.js
casper.userAgent("Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13");
casper.test.begin('bing search', 2, function(test) {
casper.start('http://www.bing.com/', function() {
this.waitUntilVisible('#sb_form_q', function() {
this.sendKeys('#sb_form_q', "book", true);
this.click('#sb_form_go');
this.waitUntilVisible('#count', function() {
var val = this.evaluate(function() {
return document.getElementById('count').innerText
});
console.log(val)
});
});
}).run(function() {
test.done();
});
});
Zombie.js
var Browser = require("zombie");
var browser = new Browser()
browser.userAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13"
browser.visit("http://www.bing.com/", function() {
browser.fill("#sb_form_q", "book");
browser.pressButton("#sb_form_go");
function resultArrived(window) {
return window.document.querySelector("#count")
}
browser.wait(resultArrived, function() {
console.log(browser.document.querySelector("#count").innerHTML)
});
});
最佳答案
我不确定你为什么不使用 zombie 的 Promise 语法(就像你对 casper 所做的那样)?你应该做类似的事情:
browser.fill(...)
.then(browser.pressButton)
.then(something else)
不使用 Promise 语法可能会导致各种奇怪的效果,因为异步 api 中的执行顺序与您在脚本语言中习惯的自上而下的代码不同。
对于你的问题,我不能完全确定,但根据我的经验,zombie.js 和 capser.js(在 phantom.js 之上)的速度非常相似。另请注意,zombie.js 文档指出:
To wait for the page to fully load and process events, you pass visit a callback function.
由于您确实传递了回调,因此您会得到您所期望的结果 - 等待整页加载。
关于phantom.js 与 zombie .js 的性能对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19203157/