urls.txt文件中包含多个图像url地址(一行一个),使用多线程下载这些图像并按顺序保存为0.jpg、1.jpg、2jpg
from urllib3 import *
import threading
http = PoolManager()
disable_warnings() # 禁用警告
f = open('demo.txt', 'r')
urllist = []
while True:
url = f.readline()
if url == '':
break
urllist.append(url.strip())
f.close()
#多线程类
class DownloadThread(threading.Thread):
def __init__(self, func, args):
super().__init__(target=func, args=args)
def download(filename, url):
response = http.request('GET', url)
f = open(filename,'wb') # wb的b表示我们要写的文件是一个二进制的文件
f.write(response.data)
f.close()
print('<',url,'>','下载完成。')
for i in range(len(urllist)):
thread = DownloadThread(download, (str(i) + '.jpg', urllist[i]))
thread.start()