如何在Node.js中使用JQuery选择器

如何在Node.js中使用JQuery选择器,jquery,node.js,jquery-selectors,Jquery,Node.js,Jquery Selectors,我正试图从硬盘中的HTML文件中提取电子邮件信息 如果我在firefox中加载文件并运行jquery bookmarklet,我可以成功地使用以下选择器/函数 window.jQuery("a.iEmail").each(function(el) { console.log(window.jQuery(this).attr('href')) }); 但是在Node.js中使用它是不起作用的 var document = require("jsdom").jsdom(), script

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

如果我在firefox中加载文件并运行jquery bookmarklet,我可以成功地使用以下选择器/函数

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

在node.js中使用jquery很困难,但这是可能的。下面是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
});
var jsdom=require('jsdom').jsdom,
sys=require('sys'),
window=jsdom().createWindow();
jQueryify(窗口,'/path/to/jquery.js',函数(窗口,jquery){
window.jQuery('body').append(“helloworld”);
sys.puts(window.jQuery(“.testing”).text();//输出Hello World
});
有关更多信息,请参阅:

或:


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

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

jsdom
支持带有“官方”方式的
jQuery

简单代码:

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

}

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

您可以轻松使用:

完整示例:

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'));
  });
});
Cheerio特别适合这样做:我明白了
$('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'));
  });
});