多线程批量下载图片
"""
download函数获取请求
done 下获取的请求写入到本地
使用线程池
"""
点击查看代码
import os
import requests
from concurrent.futures import ThreadPoolExecutor
def download(image_url):
res = requests.get(
url=image_url,
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
}
)
return res
def outer(filename): # 闭包
def save(respons): # 将download返回值封装成respons ,利用respons.result() 获取值
res = respons.result()
if not os.path.exists("images"):
os.makedirs("images")
filepath = os.path.join("images", filename)
with open(filepath, mode='wb') as image_object:
image_object.write(res.content)
return save
pool = ThreadPoolExecutor(10)
with open('mv.csv', mode='r', encoding='utf-8') as file_object:
f = file_object.readlines()
for line in f:
# print(line.strip().split(','))
num, filename, url = line.strip().split(',') # 注意使用strip()去除每行的换行符,否则请求会出现404错误。
filename = "{}.png".format(filename)
print(url)
fur = pool.submit(download, url)
fur.add_done_callback(outer(filename))