python 下载图片

import os
import random
import uuid,json
from concurrent.futures import ThreadPoolExecutor
BASE_DIR = os.path.dirname((os.path.abspath(__file__)))


def urllib_download(IMAGE_URL,name):
    from urllib.request import urlretrieve
    try:
        urlretrieve(IMAGE_URL, './image/%s.jpg'% name)
        print(name)
    except Exception as e:
        pass


if __name__ == '__main__':
    os.makedirs('./image/', exist_ok=True)
    pool = ThreadPoolExecutor(3)               # 定义3个线程
    with open("img.work.cn.txt", "r",encoding="utf-8") as tx_new:
        for z in tx_new:
            # IMAGE_URL = "https://www.jenkins.io/images/post-images/2021/jenkins-awards-2021.png"
            IMAGE_URL = z
            name = uuid.uuid1()
            pool.submit(urllib_download, IMAGE_URL,name)
posted @ 2021-08-06 17:19  叨客厨子  阅读(43)  评论(0编辑  收藏  举报