码迷,mamicode.com
首页 > 编程语言 > 详细

多线程下载图片

时间:2019-06-01 14:01:36      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:多线程下载   reading   linu   name   content   upload   ima   file   images   

import requests,threading,time
from hashlib import md5

urls = [
    http://www.178linux.com/wp-content/uploads/2018/02/5.jpg,
    http://img1.imgtn.bdimg.com/it/u=1139158180,2224775217&fm=11&gp=0.jpg,
    http://www.linuxidc.com/upload/2019_04/19041915053582.png,
    http://www.veryxue.com/file/upload/201905/09/201804031578.jpg,
    http://5b0988e595225.cdn.sohucs.com/images/20171209/8e81dcb041a9425c823daf6b6053e03b.jpg
]
result=[]
def down_load_picture(url):
    r=requests.get(url)
    file_name=md5(r.content).hexdigest()
    with open(file_name+".jpg",wb) as fw:
        fw.write(r.content)
        result.append(file_name)
        print("[%s]下载完成"%file_name)

start_time = time.time()
for url in urls:
    #down_load_picture(url)
    t=threading.Thread(target=down_load_picture,args=(url,))
    t.start()

while threading.activeCount()!=1:  # 判断当前系统是否只有一个主线程
    pass
end_time = time.time()
print(result)
run_time= end_time - start_time
print("运行的时间:%s"%run_time)

 

多线程下载图片

标签:多线程下载   reading   linu   name   content   upload   ima   file   images   

原文地址:https://www.cnblogs.com/skyxiuli/p/10959347.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!