【问题标题】:How to use JQuery selectors in Node.js如何在 Node.js 中使用 JQuery 选择器
【发布时间】:2011-05-17 00:12:39
【问题描述】:

我正在尝试从我硬盘中的 HTML 文件中提取电子邮件信息。

如果我在 Firefox 中加载文件并运行 jQuerify 小书签,我可以成功使用以下选择器/函数

window.jQuery("a.iEmail").each(function(el) {
  console.log(window.jQuery(this).attr('href'))
});

但是在 Node.js 中使用它是行不通的

var document = require("jsdom").jsdom(),
  script = document.createElement("script"),
  fs = require('fs');

fs.readFile('file_1.html', 'utf-8', function(err, data){
  if (err) {
    throw err;
  }

  // This output the document
  //console.log(data)

  var window = document.createWindow(data);

  script.src = 'http://code.jquery.com/jquery-1.4.2.js';
  script.onload = function() {
    console.log(window.jQuery.fn.jquery);
    // outputs: 1.4.2
    //console.log(window.jQuery);

    /*
     * This line works if i load the local file in firefox and execute
     * the jQuerify bookmarlet
     */
    window.jQuery("a.iEmail").each(function(el) {
      console.log(window.jQuery(this).attr('href'))
    });
  };
  document.head.appendChild(script);
});

【问题讨论】:

标签: jquery node.js jquery-selectors


【解决方案1】:

使用 Cheerio

Cheerio 是核心 jQuery 的服务器实现,非常适合使用选择器。

您可以轻松使用the each function

$('a.iEmail').each(function (i, elem) {
  console.log($(this).attr('href'));
});

完整示例:

var fs = require('fs');
var cheerio = require('cheerio');

fs.readFile('file_1.html', 'utf-8', function (err, data) {
  if (err) {
    throw err;
  }

  var $ = cheerio.load(data);

  $('a.iEmail').each(function (i, elem) {
    console.log($(this).attr('href'));
  });
});

【讨论】:

    【解决方案2】:

    jsdom 支持“官方”方式的jQuery

    jsdom.env(string, [scripts], [config], callback);
    

    简单代码:

    var jsdom = require("jsdom"),
    fs = require('fs');
    
    fs.readFile('file_1.html', 'utf-8', function (err, data) {
        if (err) {
            throw err;
        }
        jsdom.env(data, ["http://code.jquery.com/jquery.js"], function (errors, window) {
            var $ = window.$;
            $("a.iEmail").each(function() {
                console.log(this.href)
            });
        })
    
    }
    

    https://github.com/tmpvar/jsdom#easymode

    【讨论】:

      【解决方案3】:

      我现在知道问题出在哪里了。

      html数据,必须在文档创建调用中传递,所以代码如下:

      var jsdom = require("jsdom"),
          fs = require('fs');
      
      fs.readFile('file_1.html', 'utf-8', function(err, data){
        if (err) {
          throw err;
        }
      
        // This output the document
        //console.log(data)
      
        // HTML data should be in document creation call
        var document = jsdom.jsdom(data); // data is the html content
        var script = document.createElement("script");
      
        // HTML data SHOULD NOT be in window creation call
        var window = document.createWindow();
      
        script.src = 'http://code.jquery.com/jquery-1.4.2.js';
        script.onload = function() {
          console.log(window.jQuery.fn.jquery);
          // outputs: 1.4.2
          //console.log(window.jQuery);
      
          /*
           * This line works if i load the local file in firefox and execute
           * the jQuerify bookmarlet
           */
          window.jQuery("a.iEmail").each(function(el) {
            console.log(window.jQuery(this).attr('href'))
          });
        };
        document.head.appendChild(script);
      });
      

      【讨论】:

        【解决方案4】:

        很难将 jquery 与 node.js 一起使用,但它是可能的。这是 jsdom 的实现:

        var jsdom = require('jsdom').jsdom,
            sys = require('sys'),
            window = jsdom().createWindow();
        
        jsdom.jQueryify(window, '/path/to/jquery.js', function (window, jquery) {
          window.jQuery('body').append("<div class='testing'>Hello World</div>");
          sys.puts(window.jQuery(".testing").text()); // outputs Hello World
        });
        

        更多信息见:

        http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs

        或:

        Can I use jQuery with Node.js?

        【讨论】:

          猜你喜欢
          • 2011-12-26
          • 2012-03-07
          • 2012-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-08
          相关资源
          最近更新 更多