我有一个文件夹的链接,里面有大量我想要下载的文件。我开始一次下载一个文件,但是花了很长时间。有没有办法产生一些多线程进程来同时下载一批文件。可能就像process1下载文件夹中的前20个文件一样,process2同时下载下20个文件,以此类推。
现在,我正在做如下工作:
import urllib, os
os.chdir('/directory/to/save/the/file/to')
url = 'http://urltosite/folderthathasfiles
urllib.urlretrieve(url)
发布于 2017-07-12 07:07:46
您可以定义一个以link
和filenames
的list
为参数的function
,然后它将遍历list
并下载files
,然后为每个list
创建一个list
并使其指向function
。例如:
def download_files(url, filenames):
for filename in filenames:
urllib.urlretrieve(os.path.join(url,filename))
# then create the lists and threads
url = 'test.url'
files = [[file1, file2, file3....], [file21, file22, file23...]...]
for lst in files:
threading.Thread(target=download_files, args=(url, lst)).start()
https://stackoverflow.com/questions/45045787
复制相似问题