我的目标是从文档中获取所有图像,然后将所有大于 150x150px 的图像下载到本地。
我被困在从先前步骤中获得的 URL 中检索文件。这是错误的代码行(完整代码 - 最后):
...
var copyResult = fs.copy(imagesURLs[i], destFile);
...
当我从控制台运行时,它只是挂在 fs.copy() 上,没有任何错误。
据我所知, fs.copy() 不适用于远程 URL,即使您设置了所有正确的参数(--load-images=yes,--local-to-remote-url-access=yes)。我是对的还是我在 copy() 上做错了什么?是否有任何方法可以直接从 webkit 的缓存中获取文件?
获得了最新的 phantomjs 版本和 ubuntu 服务器。
我将不胜感激任何帮助。
完整的脚本代码:
if (phantom.args.length < 1 || phantom.args.length > 2)
{
console.log('Usage: phantomjs ' + phantom.scriptName + ' <URL>');
phantom.exit();
}
else
{
var page = new WebPage(),
address = phantom.args[0];
page.viewportSize = { width: 1200, height: 4000 };
page.open(address, function (status)
{
if (status === 'success')
{
var imagesURLs = page.evaluate(function ()
{
var documentImages = [], imagesCount = document.images.length, index = 0;
while (index < imagesCount)
{
if ((document.images[index].width >= 150) && (document.images[index].height >= 150))
{
documentImages.push(document.images[index].src);
}
index++;
}
return documentImages;
});
var fs = require('fs');
for (var i in imagesURLs)
{
var fileName = imagesURLs[i].replace(/^.*[\\\/]/, '');
var destFile = '' + fs.workingDirectory + '/www/images/' + fileName;
console.log(destFile);
var copyResult = fs.copy(imagesURLs[i], destFile);
console.log(copyResult);
}
}
else
{
console.log('status: ' + status);
}
phantom.exit();
});
}