使用jsdom时出现“Dispatching event'DOMNodeInsertedIntoDocument'failed”错误

时间:2013-08-22 06:52:53

标签: node.js web-scraping jsdom

我正在考虑将JSDom用于需要抓取网站的项目 我开始尝试亚马逊页面。这是一个示例代码:

jsdom.env(url, ["http://code.jquery.com/jquery.js"], function(errors, window) {
    console.log(errors);
    var $ = window.$,
        results = parseResultsPage($);
    //do some stuff
    window.close();
});

起初,我有一个if(errors.length > 0) ...条款,但事实证明,errors总是满的。即使抓取本身有效,而且我得到了我需要的所有结果,我总是得到:

[ { type: 'error',
    message: 'Dispatching event \'DOMNodeInsertedIntoDocument\' failed',
    data: { error: [Object], event: [Object] } } ]

这意味着我无法有效地测试错误。简单地忽略这个错误对我来说是不安全的。

有什么建议吗?这可能是与亚马逊有关的问题吗? (他们在页面上使用jQuery 1.2.6)

更新
在JSDom github页面(link)上提交了问题。

1 个答案:

答案 0 :(得分:3)

好吧,在使用node-inspector进行调试会话之后,我设法在Amazon页面上挑出了一段抛出该错误的代码。 这是一个长内联<style>元素内的CSS规则,JSDom不知道如何处理:

<style type="text/css">
...
.cust-rec-aui-button @-moz-document url-prefix(){
    .cust-rec-aui-button .a-button .a-button-text{
        line-height:29px
    }

    .cust-rec-aui-button .a-button.a-button-small .a-button-text{
        line-height:21px
    }

}
...
</style>

起初,我认为这是一个CSS语法错误(虽然JSDom不应该为那些引发异常),但后来我发现一些来源(here's one)说这是完全合法的。

因此,在与JSDom的开发人员(see issue on Github进行协商以获得完整的通信以及重现该问题的代码)之后,它已被宣布为错误,并且希望能够得到修复!