PhantomJS运行到前一段时间,但突然间它无法正常工作。
我尝试捕获此网站,但未加载图片并保存屏幕截图。对于这些站点,有数据要导入Ajax,
在网站加载期间:
TypeError:undefined不是对象(评估' naver.main.myteam.init')。
似乎这个错误不会导致语法在Ajax中调用Data。
请参阅随附的代码和错误文本。我将附上截图文件和我想要的结果截图。
var resourceWait = 300,
maxRenderWait = 10000,
url = 'https://m.naver.com';
var page = require('webpage').create(),
count = 0,
forcedRenderTimeout,
renderTimeout;
page.settings.javascriptEnabled = true;
page.settings.webSecurityEnabled = false;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1';
function doRender(){
page.render('naver-mobile.png');
phantom.exit();
}
page.onResourceRequested = function(req){
count += 1;
console.log('> ' + req.id + ' - ' + req.url);
clearTimeout(renderTimeout);
};
page.onResourceReceived = function(res){
if(!res.stage || res.stage === 'end'){
count -= 1;
console.log(res.id + ' ' + res.status + ' - ' + res.url);
if(count === 0){
renderTimeout = setTimeout(doRender, resourceWait);
}
}
};
page.open(url, function(status){
if(status !== "success"){
console.log('Unable to load url');
phantom.exit();
}else{
forcedRenderTimeout = setTimeout(function(){
console.log(count);
doRender();
}, maxRenderWait);
}
});
屏幕截图文件失败:
我想要结果截图文件:
答案 0 :(得分:1)
你不能这样做,因为PhantomJS没有window.Audio
功能:
ReferenceError: Can't find variable: Audio
它会导致许多错误..
您需要使用slimerjs - 它具有几乎相同的API,并且在FireFox之上工作,您还需要安装xvfb才能使用它。
# xvfb Linux Example:
pkill [X,x]vfb; pkill nw; Xvfb :1 -screen 1 1440x900x24 >/dev/null 2>&1 &
# Adding the symbolic links:
ln -sf /path/to/firefox /usr/local/bin -v; \
ln -sf /path/to/slimerjs /usr/local/bin -v
export DISPLAY=:1.1; slimerjs test.js >>/dev/stdout
我测试了以下SlimerJS脚本,它完美运行:
function on_init (page){
page.viewportSize = {width:1024,height:768}
page.evaluate(function (){
screen = {width:1024,height:768,availWidth:1024,availHeight:768};
innerWidth=1024; innerHeight=768; outerWidth=1024; outerHeight=768;
window.navigator = {
plugins: {length: 2, 'Shockwave Flash': {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}},
mimeTypes: {length: 2, "application/x-shockwave-flash": {description: "Shockwave Flash", suffixes: "swf", type: "application/x-shockwave-flash", enabledPlugin: {name: 'Shockwave Flash', filename: '/usr/lib/flashplugin-nonfree/libflashplayer.so', description: 'Shockwave Flash 11.2 r202', version: '11.2.202.440'}}},
appCodeName: "Mozilla",
appName: "Netscape",
appVersion: "5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
cookieEnabled: 1,
languages: "en-US,en",
language: "en",
onLine: 1,
doNotTrack: null,
platform: "Linux x86_64",
product: "Gecko",
vendor: "Google Inc.",
vendorSub: "",
productSub: 20030107,
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
geolocation: {getCurrentPosition: function getCurrentPosition(){},watchPosition: function watchPosition(){},clearWatch: function clearWatch(){}},
javaEnabled: function javaEnabled(){return 0} };});};
var page = require('webpage').create(); page.onInitialized=function(){on_init(page)}
page.open('https://m.naver.com',function(status){
setTimeout(function(){
page.render('naver-mobile.png');phantom.exit();console.log('Done!');
},5000);
});