无法解析格式不正确的 HTML
这是我的 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 格式不正确。真的是因为这个吗?如果是的话有办法解决吗?
谢谢
Here is my node.js program:
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.$);
});
and the output:
/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)
I think it doesn't work because the HTML from the source page is not correctly formated. Is it really because of that and is there a way to fix it if it is ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止,jsdom 似乎只支持 DOM Level 1 和一些常见的浏览器提供的方法,但
documentElement
来自 DOM Level 2 规范。参考:
Well it seems that jsdom till now supports only DOM Level 1 and some common browser provided methods, but
documentElement
is from DOM Level 2 spec.Ref: