如何忽略phantomjs中的错误

use*_*048 24 javascript phantomjs

我有一个网络爬虫,我使用phantomjs来解析页面,我想得到html,但我总是在html代码输出之前得到这种类型的错误

ReferenceError: Can't find variable: collapse_content_selector

  http://staticloads.com/js/toggle.js?v=2013.10.04:135
TypeError: 'undefined' is not a function (evaluating '$('[placeholder]').placeholderLabel()')
Run Code Online (Sandbox Code Playgroud)

我怎么能阻止它

Cyb*_*axs 41

最简单的方法是向phantom.onerrorwebpage.onerror添加错误处理程序.当存在JavaScript执行错误时(在页面中或在脚本中),将调用这些回调.

page.onError = function(msg, trace) {
    var msgStack = ['ERROR: ' + msg];
    if (trace && trace.length) {
        msgStack.push('TRACE:');
        trace.forEach(function(t) {
            msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function + '")' : ''));
        });
    }
    // uncomment to log into the console 
    // console.error(msgStack.join('\n'));
};
Run Code Online (Sandbox Code Playgroud)