【发布时间】: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);
});
【问题讨论】:
-
Cheerio 特别适合:D 见 stackoverflow.com/a/33654529/1480391
标签: jquery node.js jquery-selectors