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++;
}
Hy vọng điều này helps. Để biết thêm thông tin về các cuộc gọi FileSystem, hãy xem trang này: http://phantomjs.org/api/fs/
Ngoài ra, tôi muốn thêm, tôi tin rằng các chức năng FileSystem chỉ khả dụng trong PhantomJS 1.3 trở lên. Vui lòng đảm bảo chạy phiên bản latest. Tôi đã sử dụng PyPhantomJS cho Windows nhưng tôi chắc chắn rằng điều này sẽ hoạt động mà không bị cản trở trên các hệ thống khác.
Nguồn
2011-09-26 00:13:51
Bằng cách nào đó tôi chỉ nhận được 'PNG' trống với tập lệnh của bạn. Thêm 'console.log (page.content);' trong 'page.onLoadFinished' trả về'
'bất kể nội dung HTML thực là… Bất kỳ đề xuất nào? Phantomjs v1.9.2 – AvLBạn có chắc chắn rằng các trang của bạn đang tải hoàn toàn trước khi bạn định chụp nhanh không? –
Tôi cũng đã kiểm tra trạng thái '. Xem câu hỏi của tôi: http://stackoverflow.com/questions/19939046/phantomjs-fails-to-open-local-file – AvL