多线程下载图片

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)

 

转载于:https://www.cnblogs.com/skyxiuli/p/10959347.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值