2017-05-24 153 views
0

我是新来的一般使用CasperJS和JavaScript。目前,我正试图学习如何从git仓库下载文件。在阅读参考页面时,我偶然发现了CasperJS的下载方法。其结果是,我已经试过如下: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); 
}); 

的问题,这是不是在指定的路径下载只是文件到我的电脑,它不是复制部分的目录路径是相同的绝对创建的路径(没有它们的内容)。我不知道我做了什么是错的(我只能这样假设),或者是什么,但是有人能帮我吗?

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

+0

此外,使用waitForUrl方法后,它表示有5000毫秒的等待超时,因此退出。我只能假设这与问题有关。仍然环顾四周。 –

+0

^试图将等待超时时间增加到20秒,但仍然不起作用。所以我认为这不是问题。 –

回答

0

好吧,在经过长时间的休息并回到它之后,我意识到这是一个容易犯的错误。首先,我将两个绝对路径连接在一起,这就是为什么它将目录复制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(); 
相关问题