我想使用phantomjs截圖很多頁面。屏幕截圖很多網頁
的問題是如何處理,如果我有1000多頁,因爲在下面的代碼當下崩潰phantomjs:
這是aobj一個例子:
var aobj = '[{"kb":21.047829999999976,"jb":52.174250000000015,"ff":110.16456426650427},{"kb":21.047997078651633,"jb":52.17421235955058,"ff":110.16456426650427},{"kb":21.048164157303404,"jb":52.17417471910114,"ff":110.16456426650427},{"kb":21.04833123595506,"jb":52.1741370786517,"ff":110.16456426650427},{"kb":21.048498314606718,"jb":52.174099438202255,"ff":110.16456426650427},{"kb":21.04866539325849,"jb":52.17406179775282,"ff":110.16456426650427},{"kb":21.048832471910146,"jb":52.17402415730338,"ff":110.16456426650427},{"kb":21.048999550561803,"jb":52.173986516853944,"ff":110.16456426650427}]'
...和代碼的更
休息:
aobj = JSON.parse(aobj);
function tes_par(nrr,jbb,kkb,hhf)
{
var page = require('webpage').create();
page.viewportSize = { width: 600, height: 480 };
console.log(nrr);
page.open('http://maps.googleapis.com/maps/api/streetview?size=640x480&location='+jbb+','+kkb+'&pitch=-0.760&sensor=false&heading='+hhf, function()
{
page.render(nrr + '.png');
});
}
for (var i=0;i<aobj.length;i++)
{
tes_par(i,aobj[i].jb,aobj[i].kb,aobj[i].ff);
}
我認爲這是因爲JS一次只能運行一個代碼,所以它會佔用所有資源,但我不知道如何等待page.open和page.render並等待有多於1000個這些功能的調用。
我還試圖用setTimeout的,但同樣沒有成功:
aobj = JSON.parse(aobj);
function tes_par(nrr,jbb,kkb,hhf)
{
var page = require('webpage').create();
page.viewportSize = { width: 600, height: 480 };
console.log(nrr);
page.open('http://maps.googleapis.com/maps/api/streetview?size=640x480&location='+jbb+','+kkb+'&pitch=-0.760&sensor=false&heading='+hhf, function()
{
page.render(nrr + '.png');
});
}
function aa_bb(ii)
{
var jj=0;
(ii+500<aobj.length) ? jj=ii+500 : jj=aobj.length;
for (var i=ii;i<jj;i++)
{
tes_par(i,aobj[i].jb,aobj[i].kb,aobj[i].ff);
}
};
aa_bb(1);
setTimeout(function(){aa_bb(501)},120000);
對不起,我沒有在幻影js工作。你正在節點中運行這個腳本吧?你可以使用某種類型的隊列,以便一次只傳遞一個請求嗎? – karthick
是的,我一直在想這個。不幸的是,我的隊列實現不起作用:(。 – Tom