我是僵尸的新手,只是试图让一个基本的测试运行。我有以下代码:
var Browser = require('zombie');
var startTime = +new Date();
Browser.visit("http://zombie.labnotes.org/", function(e, browser) {
var duration;
console.log("Successfully visted the page");
console.log(browser.html());
duration = (+(new Date())) - startTime;
console.log("Finished in (milliseconds): " + duration);
});
出于某种原因,我回到控制台的是:
成功访问该页面
<html>
<head></head>
<body></body>
</html>
Finished in (milliseconds): 5020
这显然不是正确的标记,这需要相当长的时间(5秒)。有什么想法吗?
UPDATE:最终使用request和jsdom切换到更简单的模型。这是我使用的代码: var request = require('request'), jsdom = require('jsdom');
//Tell the request that we want to fetch youtube.com, send the results to a callback function
request({uri: 'http://youtube.com'}, function(err, response, body){
var self = this;
self.items = [];
//Just a basic error check
if(err && response.statusCode !== 200){console.log('Request error.');}
//Send the body param as the HTML code we will parse in jsdom
//also tell jsdom to attach jQuery in the scripts and loaded from jQuery.com
jsdom.env({
html: body,
scripts: ['http://code.jquery.com/jquery-1.6.min.js']
}, function(err, window){
//Use jQuery just as in a regular HTML page
var $ = window.jQuery;
console.log(body);
});
});
取自:http://net.tutsplus.com/tutorials/javascript-ajax/how-to-scrape-web-pages-with-node-js-and-jquery/
但我仍然想知道Zombie出了什么问题,因为我想用它来测试其他项目。
答案 0 :(得分:1)
浏览器是通过require加载的类。您想要创建一个作为Browser实例的变量,然后使用该变量调用visit。你的代码应该是:
var Browser = require('zombie');
var startTime = +new Date();
my_browser = new Browser(); // Here's where you need to call new
my_browser.visit("http://zombie.labnotes.org/", function(e, browser) {
var duration;
console.log("Successfully visted the page");
console.log(browser.html());
duration = (+(new Date())) - startTime;
console.log("Finished in (milliseconds): " + duration);
});
答案 1 :(得分:0)
现在似乎已经升级到Node.js和Zombie.js的新版本。请注意,您不能使用带有zombie.js的Node的预版本(其中一个依赖项将无法说明)。
使用NVM安装最新的稳定版本(撰写本文时版本为0.8.9)。