我已经开始在Windows上使用Phantom JS,但我在查找有关其功能的文档(可能是问题的根源)时遇到了一些困难。
使用Phantom JS,我想做以下工作:
我确信这可能是可能的,但我无法找到Phantom JS函数调用:
发布于 2011-09-26 00:13:51
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++;
}希望这能有所帮助。有关FileSystem调用的更多信息,请查看此页面:http://phantomjs.org/api/fs/
另外,我想补充的是,我认为FileSystem函数只在PhantomJS 1.3或更高版本中可用。请确保运行最新版本版本。我在Windows上使用了PyPhantomJS,但我确信这在其他系统上也能正常工作。
https://stackoverflow.com/questions/7531601
复制相似问题