
python
import requests
from concurrent.futures import ThreadPoolExecutor
def download_file(url, filename):
response = requests.get(url)
with open(filename, 'wb') as f:
f.write(response.content)
def main():
urls = ['https://example.com/file1.zip', 'https://example.com/file2.zip'] 替换为实际下载链接列表
filenames = ['file1.zip', 'file2.zip'] 替换为实际文件名列表
with ThreadPoolExecutor() as executor:
for url, filename in zip(urls, filenames):
executor.submit(download_file, url, filename)
if __name__ == '__main__':
main()
在这个示例中,我们首先定义了一个`download_file`函数,它接受一个URL和一个文件名作为参数,然后使用`requests`库获取文件内容并将其写入到指定的文件中。接下来,在`main`函数中,我们创建了一个`ThreadPoolExecutor`实例,并使用它来提交多个`download_file`任务。这样,每个URL都会由一个单独的线程处理,从而加快下载速度。