var page = require('webpage').create(), loadInProgress = false, fs = require('fs');
var htmlFiles = new Array();
console.log(fs.workingDirectory);
var curdir = fs.list(fs.workingDirectory);
// loop through files and folders
for(var i = 0; i< curdir.length; i++)
{
var fullpath = fs.workingDirectory + fs.separator + curdir[i];
// check if item is a file
if(fs.isFile(fullpath))
{
// check that file is html
if(fullpath.indexOf('.html') != -1)
{
// show full path of file
console.log('File path: ' + fullpath);
htmlFiles.push(fullpath);
}
}
}
console.log('Number of Html Files: ' + htmlFiles.length);
// output pages as PNG
var pageindex = 0;
var interval = setInterval(function() {
if (!loadInProgress && pageindex < htmlFiles.length) {
console.log("image " + (pageindex + 1));
page.open(htmlFiles[pageindex]);
}
if (pageindex == htmlFiles.length) {
console.log("image render complete!");
phantom.exit();
}
}, 250);
page.onLoadStarted = function() {
loadInProgress = true;
console.log('page ' + (pageindex + 1) + ' load started');
};
page.onLoadFinished = function() {
loadInProgress = false;
page.render("images/output" + (pageindex + 1) + ".png");
console.log('page ' + (pageindex + 1) + ' load finished');
pageindex++;
}
希望這helps。有關FileSystem調用的更多信息,請查看此頁面:http://phantomjs.org/api/fs/
另外,我想補充一點,我相信FileSystem函數僅在PhantomJS 1.3或更高版本中可用。請確保運行latest版本。我在Windows上使用了PyPhantomJS,但我相信這樣也可以在其他系統上順利運行。
不知何故,我只是用腳本得到空白的'PNG's。添加'console.log(page.content);'in'page.onLoadFinished'返回'
'無論想要真正的HTML內容是...任何建議? Phantomjs v1.9.2 – AvL在你試圖拍攝快照之前,你確定你的頁面已經完全加載嗎? –
我也檢查過「狀態」。看到我的問題:http://stackoverflow.com/questions/19939046/phantomjs-fails-to-open-local-file – AvL