我用Phantomjs到使用JavaScript和Ajax加载动态内容刮网站。
我有以下代码:
var page = require('webpage').create();
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 +'")' : ''));
});
}
console.error(msgStack.join('\n'));
};
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.open('http://www.betexplorer.com/soccer/germany/oberliga-bayern-sud/wolfratshausen-unterhaching-ii/x8rBMAB8/', function () {
console.log(page.content);
phantom.exit();
});
问题是,这个代码不获取源代码,我想。
如果您通过Web浏览器(如铬)输入网址并阅读页面的源代码(动态源代码,JavaScript和Ajax调用作了后),你会看到Web浏览器的源代码和Phantomjs源代码是完全不同的。
但在这种情况下,我需要网络浏览器的源代码。
通常这Phantomjs代码检索源代码,我需要,但在这个网址的情况下(任何其他许多人)Phantomjs不检索正确的源代码。
我认为Phantomjs不知道如何处理JavaScript和Ajax调用,负载动态内容到这个网页。
当我运行的代码中,我得到这些错误:
ERROR: TypeError: 'undefined' is not a function (evaluating 'function(e){
this.pointer.x = e.pageX;
this.pointer.y = e.pageY;
}.bind(this)')
TRACE:
-> http://www.betexplorer.com/gres/tooltip.js?serial=1410131213: 207
-> http://www.betexplorer.com/gres/tooltip.js?serial=1410131213: 157
-> http://www.betexplorer.com/gres/tooltip.js?serial=1410131213: 310 (in function "tooltip")
-> http://www.betexplorer.com/soccer/germany/oberliga-bayern-sud/wolfratshausen-unterhaching-ii/x8rBMAB8/: 291
-> http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js: 2
-> http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js: 2
-> http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js: 2
-> http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js: 2
CONSOLE: Invalid App Id: Must be a number or numeric string representing the application id. (from line #undefined in "undefined")
CONSOLE: FB.getLoginStatus() called before calling FB.init(). (from line #undefined in "undefined")
那么我如何使用 Phantomjs获取此页面的动态源代码(http://www.betexplorer.com/soccer/germany/oberliga-bayern-sud/wolfratshausen-unterhaching-ii/x8rBMAB8/)?