CasperJS下载复制文件夹路径,而不是下载指定的文件



我是使用 CasperJS 和 JavaScript 的新手。目前,我正在尝试学习如何从 git 存储库下载文件。在阅读参考页面时,我偶然发现了CasperJS的下载方法。因此,我尝试了以下方法:

var casper = require('casper').create();
var url = null;
var utils = require('utils');
var http = require('http');
var fs = require('fs');
// A test to make sure that we can go into a github without any authentication
casper.start('https://github.com/gabegomez014/Test2', function(){
this.echo(this.getTitle(), 'INFO');
});
// A test for how the download function works
casper.then(function(){
url = 'https://github.com/gabegomez014/Test2/blob/master/.gitignore';
var cwd = fs.absolute('.');
var parent = fs.absolute("../");
var path = cwd + parent;
this.download(url, path);
});
// A test in order to get the current HTTP status of links that have been put into the function
// Attempt 3 works and cleaner
casper.thenOpen('https://github.com/gabegomez014/Test2/blob/master/.gitignore', function(){
var res = this.status(false);
this.echo(res.currentHTTPStatus);
});

这样做的问题是,它不是仅将文件下载到指定路径处的计算机,而是复制与创建的绝对路径相同的部分目录路径(也没有其内容)。我无法判断我做错了什么(我只能假设),还是什么,但有人可以帮助我吗?

PS:以另一种方式下载这些文件会更容易吗?因为如果有必要,它就会完成。提前感谢您的时间和帮助。

好吧,在休息了很长时间并回到它之后,我意识到这是我犯的一个容易的错误。首先,我将两条绝对路径连接在一起,这就是为什么它复制目录 2 次的原因。

在文件的下载方面,我了解到我不需要在 CasperJS 中为该方法指定路径,它会将其放在当前工作目录中。但是,添加隐藏文件时要小心,因为正如您所知,它们是隐藏的。例如,我下载了一个 .gitignore,由于它是隐藏的,直到我在终端中为我正在执行这项工作的目录完成"ls -a"之前,我才能看到它。

我还想说,即使我为文件或.txt指定了某个扩展名,例如 .java,此方法也只会下载 HTML,因此如果您希望在 git 存储库中关联实际文件,您将不得不做一些我目前仍在寻找的其他事情。以下是带有修复程序的代码:

var casper = require('casper').create();
var url = null;
var utils = require('utils');
var http = require('http');
var fs = require('fs');
casper.options.waitTimeout = 20000;
// A test to make sure that we can go into a github without any authentication
casper.start('https://github.com/gabegomez014/Test2', function(){
this.echo(this.getTitle(), 'INFO');
});
// A test for how the download function works
casper.thenOpen('https://github.com/gabegomez014/SSS', function() {
url = 'https://github.com/gabegomez014/SSS/tree/master/src/SolarSystem/'
this.download(url, 'Algorithms.java');
});
// A test in order to get the current HTTP status of links that have been put into the function
// Attempt 3 works and cleaner
casper.thenOpen('https://github.com/gabegomez014/Test2/blob/master/.gitignore', function(){
var res = this.status(false);
this.echo(res.currentHTTPStatus);
});
// To let me know that all of the functions have finished and all done
casper.then(function(){
console.log('complete');
});
casper.run();

最新更新