在urls.txt文件中包含了若干个图像url,一行一个url,请使用多线程下载这些图像文件,并按url出现的顺序保存为0.jpg、1.jpg、2.jpg,依次类推。
from urllib3 import * import threading http = PoolManager() disable_warnings() f = open('urls.txt', 'r') urlList = [] while True: url = f.readline() if url == '': break urlList.append(url.strip()) f.close() print(urlList) class DownloadThread(threading.Thread): def __init__(self, func, args): super().__init__(func, args) def donwload(filename, url): response = http.request('GET', url) f = open(filename, 'wb') f.write(response.data) f.close() print('<', url, '>', '下载完成') for i in range(len(urlList)): thread = DownloadThread(donwload, (str(i) + '.jpg', urlList[i])) thread.start()