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

8
我正在尝试从硬盘中的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);
});

Cheerio非常适合这个:D请参见https://dev59.com/zFLTa4cB1Zd3GeqPYjxI#33654529 - Yves M.
4个回答

11

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

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

1

使用Cheerio

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

您可以轻松使用each函数

$('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'));
  });
});

1

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

更多信息请参见:

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

或者:

我能在Node.js中使用jQuery吗?


0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接