Javascript Phantomjs page.content不是';无法检索页面内容
我使用Phantomjs来抓取使用JavaScript和Ajax加载动态内容的网站。Javascript Phantomjs page.content不是';无法检索页面内容,javascript,html,ajax,web-scraping,phantomjs,Javascript,Html,Ajax,Web Scraping,Phantomjs,我使用Phantomjs来抓取使用JavaScript和Ajax加载动态内容的网站。 我有以下代码: var page = require('webpage').create(); page.onError = function(msg, trace) { var msgStack = ['ERROR: ' + msg]; if (trace && trace.length) {
我有以下代码:
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浏览器(如chrome)输入URL并阅读页面的源代码(JavaScript和Ajax调用后的动态源代码),您将看到web浏览器源代码和Phantomjs源代码完全不同。
但是在这种情况下,我需要web浏览器的源代码。
通常,此Phantomjs代码检索我所需的源代码,但对于此url(任何其他url),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获取此页面()的动态源代码呢?由于该页面是动态生成的,您需要等待一段时间才能访问所需的页面源代码
page.open('http://www.betexplorer.com/soccer/germany/oberliga-bayern-sud/wolfratshausen-unterhaching-ii/x8rBMAB8/', function () {
setTimeout(function(){
console.log(page.content);
phantom.exit();
}, 5000); // 5 sec should be enough
});
TypeError:“undefined”不是一个函数
指的是bind
,因为PhantomJS 1.x不支持它。PhantomJS 1.x使用了QtWebkit的一个旧叉子,与Chrome 13或Safari 5相当。较新的PhantomJS2使用了一个支持bind
的较新引擎。如果仍然使用1.x版,则需要在事件处理程序内部添加一个垫片:
page.onInitialized = function(){
page.evaluate(function(){
var isFunction = function(o) {
return typeof o == 'function';
};
var bind,
slice = [].slice,
proto = Function.prototype,
featureMap;
featureMap = {
'function-bind': 'bind'
};
function has(feature) {
var prop = featureMap[feature];
return isFunction(proto[prop]);
}
// check for missing features
if (!has('function-bind')) {
// adapted from Mozilla Developer Network example at
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
bind = function bind(obj) {
var args = slice.call(arguments, 1),
self = this,
nop = function() {
},
bound = function() {
return self.apply(this instanceof nop ? this : (obj || {}), args.concat(slice.call(arguments)));
};
nop.prototype = this.prototype || {}; // Firefox cries sometimes if prototype is undefined
bound.prototype = new nop();
return bound;
};
proto.bind = bind;
}
});
};
摘自我的答案。ok我注册了OnConsolleMessage和OnError事件,并更新了我的问题。