如何在Node.js中使用JQueryselect器

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

如果我在firefox中加载文件并运行jQuerify书签,我可以成功地使用下面的select器/函数

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

我现在知道问题是什么。

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

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

使用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

要么:

我可以用Node.js使用jQuery吗?

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

使用Cheerio

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

您可以轻松使用每个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')); }); });