2013-10-18 86 views
23

我有一個網絡爬蟲錯誤,我用phantomjs解析頁面, 我想要得到的HTML,但是HTML代碼如何忽略phantomjs

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()') 
之前,我總是得到這個類型的錯誤輸出

我該怎麼辦?

回答

37

最簡單的方法是將錯誤處理程序添加到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')); 
};