我正在处理Node项目,其中有一系列文件,例如
class Login(unittest.TestCase):
instances = []
site_url1 = "https://www.google.com/"
site_url2 = "https://www.facebook.com/"
def openFirefox(self):
# create a new Firefox session
firefox = webdriver.Firefox()
# save session in instances
self.instances.append(firefox)
# return the session
return firefox
def setUp(self):
browser = self.openFirefox()
browser.open(self.site_url1)
def more_loggin(self):
browser = self.openFirefox()
browser.open(self.site_url2)
def tearDown(self):
for browser in self.instances:
# close every instance in instances
browser.quit()
我希望以最有效的方式在本地下载这些文件。这个数组中可能有多达几十个URL ...是否有一个好的库可以帮助我抽象出来?我需要可以在数组中调用的内容以及所需的本地目录,以进行重定向,使用http和https,智能地限制同时下载等。
答案 0 :(得分:1)
node-fetch是一个可爱的小库,它为节点带来了fetch
的功能。由于fetch
返回了承诺,因此管理并行下载非常简单。这是一个示例:
const fetch = require('node-fetch')
const fs = require('fs')
// You can expand this array to include urls are required
const urls = ['http://web.site/file1.iso', 'https://web.site/file2.pdf']
// Here we map the list of urls -> a list of fetch requests
const requests = urls.map(fetch)
// Now we wait for all the requests to resolve and then save them locally
Promise.all(requests).then(files => {
files.forEach(file => {
file.body.pipe(fs.createWriteStream('PATH/FILE_NAME.EXT'))
})
})
或者,您可以在解析时编写每个文件:
const fetch = require('node-fetch')
const fs = require('fs')
const urls = ['http://web.site/file1.iso', 'https://web.site/file2.pdf']
urls.map(file => {
fetch(file).then(response => {
response.body.pipe(fs.createWriteStream('DIRECTORY_NAME/' + file))
})
})