这是我的 node.js 程序:
var jsdom = require('jsdom');
var site = 'http://www.lefigaro.fr/international/2011/08/09/01003-20110809ARTFIG00389-un-premier-mort-dans-les-emeutes-en-grande-bretagne.php';
var jquery = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js';
jsdom.env(site, [jquery], function(error, window) {
console.log(error);
console.log(window.$);
});
和输出:
/home/matt/Desktop/nodejs/node_modules/jsdom/lib/jsdom.js:245
window.document.documentElement.appendChild(script);
^
TypeError: Cannot call method 'appendChild' of null
at /home/matt/Desktop/nodejs/node_modules/jsdom/lib/jsdom.js:245:41
at Array.forEach (native)
at /home/matt/Desktop/nodejs/node_modules/jsdom/lib/jsdom.js:230:22
at [object Object].callback (/home/matt/Desktop/nodejs/node_modules/jsdom/lib/jsdom.js:289:17)
at [object Object].<anonymous> (/home/matt/Desktop/nodejs/node_modules/request/main.js:294:21)
at [object Object].emit (events.js:64:17)
at IncomingMessage.<anonymous> (/home/matt/Desktop/nodejs/node_modules/request/main.js:281:54)
at IncomingMessage.emit (events.js:81:20)
at HTTPParser.onMessageComplete (http.js:133:23)
at Socket.ondata (http.js:1232:22)
我认为它不起作用,因为源页面中的 HTML 格式不正确。真的是因为这个吗?如果是的话,有没有办法解决它?
谢谢