并发下载多文件
main
1.定义下载的文件路径
2.调用执行下载的方法
download
1.根据url地址请求网络资源
2.在本地创建文件
3.读取网络资源并写入到本地文件
4.处理异常
from gevent import monkey
monkey.patch_all()
import urllib.request
import gevent
def download_img(img_url,file_name):
try:
# 根据url地址请求网络资源
response_data = urllib.request.urlopen(img_url)
# 在本地创建文件
with open('download/'+file_name,'wb') as f:
print(f'开始下载:{file_name}')
# 读取网络资源并写入到本地文件
while True:
file_data = response_data.read(409600)
if not file_data:
break
f.write(file_data)
# 处理异常
except Exception as e:
print(f'下载发生错误:{file_name}:{e}')
else:
print(f'下载成功:{file_name}')
def main():
# 定义下载的文件路径
img_url1 = 'https://yys.res.netease.com/pc/zt/20170731172708/data/picture/20200609/2/10/2208x1242.jpg'
img_url2 = 'https://yys.res.netease.com/pc/zt/20170731172708/data/picture/20181120/7/2208x1242.jpg'
img_url3 = 'https://yys.res.netease.com/pc/zt/20170731172708/data/picture/20181120/1/2208x1242.jpg'
img_url4 = 'https://yys.res.netease.com/pc/zt/20170731172708/data/picture/20190705/11/2208x1242.jpg'
img_url5 = 'https://yys.res.netease.com/pc/zt/20170731172708/data/picture/20200609/1/2/2208x1242.jpg'
img_url6 = 'https://yys.res.netease.com/pc/zt/20170731172708/data/picture/20200729/10/2208x1242.jpg'
img_url7 = 'https://yys.res.netease.com/pc/zt/20170731172708/data/picture/20200628/6/2208x1242.jpg'
# 调用执行下载的方法
# download_img(img_url1,'1.jpg')
gevent.joinall([
gevent.spawn(download_img, img_url1, '1.jpg'),
gevent.spawn(download_img, img_url2, '2.jpg'),
gevent.spawn(download_img, img_url3, '3.jpg'),
gevent.spawn(download_img, img_url4, '4.jpg'),
gevent.spawn(download_img, img_url5, '5.jpg'),
gevent.spawn(download_img, img_url6, '6.jpg'),
gevent.spawn(download_img, img_url7, '7.jpg'),
])
if __name__ == '__main__':
main()