【问题标题】:Zombie.js Returns Improper Page ContentZombie.js 返回不正确的页面内容
【发布时间】:2012-09-26 06:25:37
【问题描述】:

我是僵尸新手,只是想运行一个基本的测试。我有以下代码:

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 秒)才能做到这一点。有什么想法吗?

更新:最终使用 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 出了什么问题,因为我想用它来测试其他项目。

【问题讨论】:

    标签: javascript http node.js zombie.js


    【解决方案1】:

    Browser 是通过 require 加载的类。您想创建一个作为 Browser 实例的变量,然后使用该变量调用访问。你的代码应该是:

    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);
    });
    

    【讨论】:

    • 感谢您的快速回答。但不,不是这样。我试过了。你的意思是新的浏览器()。这不是红宝石:)。 visit 方法还创建了一个实例并将其传递回回调,因此这不是问题。我想这可能是在 Ubuntu 上安装的。
    【解决方案2】:

    现在我升级到 Node.js 和 Zombie.js 的新版本似乎可以工作了。请注意,您不能将 Node 的预版本与zombie.js 一起使用(其中一个依赖项会失败)。

    使用 NVM 安装最新的稳定版本(撰写本文时为 0.8.9 版)。

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2016-05-22
      相关资源
      最近更新 更多