我正在尝试操纵远程HTML并将其操作返回。我决定使用JSDOM,但无法弄清楚如何获取被操纵的HTML。有什么想法吗?
jsdom.env({
url: "http://www.cnn.com",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (err, window) {
var $ = window.$;
console.log("HN Links");
var src = $(".ghciTopStoryImage1 img").attr('src','http://lorempixel.com/396/220/');
var headline = $(".blkbigheader span").html('header');
var description = $(".blkbigheader").parent().find("p a:eq(0)").html('text');
// not working....
content =$(window.document).html();
}
});
答案 0 :(得分:14)
var jsdom = require("jsdom").jsdom;
var serializeDocument = require("jsdom").serializeDocument;
var doc = jsdom("<!DOCTYPE html>hello");
serializeDocument(doc) === "<!DOCTYPE html><html><head></head><body>hello</body></html>";
doc.documentElement.outerHTML === "<html><head></head><body>hello</body></html>";
调整上面的示例只需content = serializeDocument(window.document)
。
答案 1 :(得分:11)
有时jQuery不是答案
content = window.document.documentElement.outerHTML;