如何忽略phantomjs中的错误 [英] how to ignore errors in phantomjs
本文介绍了如何忽略phantomjs中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网络爬虫,我使用phantomjs来解析页面,
我想得到html,但我总是在html代码之前的输出中得到这种类型的错误
I have a web crawler and I use phantomjs to parse pages, I want to get the html, but I always get this type of errors in the output before the html code
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()')
如何停止
推荐答案
最简单的方法是添加一个 phantom.onerror 或 webpage.onerror 。
当存在JavaScript执行错误时(在页面或脚本中)调用这些回调。
The easiest way is to add an error handler to phantom.onerror or webpage.onerror. These callbacks are invoked when there is a JavaScript execution error (in the page or in your script).
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'));
};
这篇关于如何忽略phantomjs中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文