How to parse html in node.js using jquery 2.1.3? -
i trying use node.js jquery 2.1.3 , jsdom 4.0.4 web scraping. running problems running jquery module. code
// load http module create http server. var $ = require('jquery')(require("jsdom").jsdom().parentwindow); var http = require('http'); var request = require("request"); // configure our http server respond hello world 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 127.0.0.1 server.listen(8000); // put friendly message on terminal console.log("server running @ http://127.0.0.1:8000/");
and error i'm getting is:
c:\users\me\desktop\apps\waerl\node_modules\jquery\dist\jquery.js:28 if ( !w.document ) { ^ typeerror: cannot read property 'document' of undefined @ module.exports (c:\users\me\desktop\apps\waerl\node_modules\jquery\dist \jquery.js:28:12) @ object.<anonymous> (c:\users\me\desktop\apps\waerl\server.js:2:26) @ module._compile (module.js:460:26) @ object.module._extensions..js (module.js:478:10) @ module.load (module.js:355:32) @ function.module._load (module.js:310:12) @ function.module.runmain (module.js:501:10) @ startup (node.js:129:16) @ node.js:814:3
does know how fix this?
thanks
jsdom 4.0.0 switched non-standard parentwindow standard defaultview property on document.
try this:
// load http module create http server. var $ = require('jquery')(require("jsdom").jsdom().defaultview); var http = require('http'); var request = require("request"); // configure our http server respond hello world 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 127.0.0.1 server.listen(8000); // put friendly message on terminal console.log("server running @ http://127.0.0.1:8000/");
Comments
Post a Comment