import requests
import threading
import hashlib
import time
def down_pic(url):
print("开始下载",url)
r = requests.get(url)
file_name = hashlib.md5(url.encode()).hexdigest() + '.jpg'
with open(file_name, 'wb') as fw:
fw.write(r.content)
print("%s下载结束"%url)
urls = ['http://sky.nnzhp.cn/static/avatar/default.jpg',
'http://sky.nnzhp.cn/static/avatar/labi.jpg',
'http://sky.nnzhp.cn/static/avatar/login.jpg',
'http://sky.nnzhp.cn/static/avatar/%E7%8B%97%E5%A4%B42.jpg',
'http://sky.nnzhp.cn/static/avatar/wsj.jpg',
'http://sky.nnzhp.cn/static/avatar/demo1.png'
]
#单线程
# start_time = time.time()
# for url in urls:
# down_pic(url)
# print("下载用时",time.time() - start_time)
#多线程
start_time = time.time()
for url in urls:
t = threading.Thread(target=down_pic,args=[url])
t.start()
while threading.activeCount()!=1:
pass
print("下载用时",time.time() - start_time)