如何将 JS 输出保存到文件

How to save the JS output to a file

本文关键字:存到文件 输出 JS      更新时间:2023-09-26

我有以下使用phantomjs运行的JS。

var webPage = require('webpage');
var system = require('system');
var page = webPage.create();
page.customHeaders = {
  "pragma": "akamai-x-feo-trace"
};
if (system.args.length === 1) {
    console.log('Try to pass some args when invoking this script!');
} else {
    page.open(system.args[1], function (status) {
        var content = page.content;
        console.log('Content: ' + content);
        phantom.exit();
    });
}

我想将console.log('Content: ' + content);的输出保存到带有./html/<random#>.html的文件夹中。

我应该怎么做?

似乎您只想保存页面内容。您可以使用文件系统模块将内容写入文件。可以通过调用 ''+(Math.random()*100000000000000000)+'.html' 生成随机文件名。