如何使用jquery 2.1.3解析node.js中的html?

时间:2015-03-21 05:43:01

标签: jquery node.js jsdom

我正在尝试使用带有jquery 2.1.3和jsdom 4.0.4的node.js来进行网络抓取。但我遇到运行jquery模块的问题。这是代码

// Load the http module to create an http server.
var $ = require('jquery')(require("jsdom").jsdom().parentWindow); 
var http = require('http');
var request = require("request");


// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/plain"});


    var uri = "http://www.forever21.com/shop/ca/en/men-tees-tanks";

    request({
        uri: uri,
    }, function(error, response, body) {
        if (error) {
            return console.error(error);
        }

        var f = $(body);

        res.write(body);

        res.end();
    });

});


// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

我得到的错误是:

C:\Users\ME\Desktop\Apps\Waerl\node_modules\jquery\dist\jquery.js:28
                                if ( !w.document ) {
                                       ^
TypeError: Cannot read property 'document' of undefined
    at module.exports (C:\Users\ME\Desktop\Apps\Waerl\node_modules\jquery\dist
\jquery.js:28:12)
    at Object.<anonymous> (C:\Users\ME\Desktop\Apps\Waerl\server.js:2:26)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

有谁知道如何解决这个问题?

由于

1 个答案:

答案 0 :(得分:0)

jsdom 4.0.0 switched从非标准parentWindow到文档上的标准defaultView属性。

试试这个:

// Load the http module to create an http server.
var $ = require('jquery')(require("jsdom").jsdom().defaultView); 
var http = require('http');
var request = require("request");


// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/plain"});


    var uri = "http://www.forever21.com/shop/ca/en/men-tees-tanks";

    request({
        uri: uri,
    }, function(error, response, body) {
        if (error) {
            return console.error(error);
        }

        var f = $(body);

        res.write(body);

        res.end();
    });

});


// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");