nodeJS中的JSDOM:如何取回被操纵的html?

Guy*_*Guy 10 javascript node.js jsdom

我试图操纵远程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();

        }
    });
Run Code Online (Sandbox Code Playgroud)

Dom*_*nic 15

来自jsdom自述文件:

var JSDOM = require("jsdom").JSDOM;

var jsdom = new JSDOM("<!DOCTYPE html>hello");

jsdom.serialize() === "<!DOCTYPE html><html><head></head><body>hello</body></html>";
doc.documentElement.outerHTML === "<html><head></head><body>hello</body></html>";
Run Code Online (Sandbox Code Playgroud)

那么适应你上面的例子就是这样content = jsdom.serialize().


Jar*_*a X 11

有时jQuery不是答案

content = window.document.documentElement.outerHTML;
Run Code Online (Sandbox Code Playgroud)