Python 线程下载字典列表

2024-04-19 00:10:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个1000本字典的列表,每本字典包含url,文件名

for di in images_to_download:
    temp = download_image(di['img_url'],di['image_full_name'])
    if not temp:
        continue
    upload_file(temp,t4['id'])



def download_image(img_url,image_full_name)
    try:
        req = request(img_url, headers=headers)
        raw_img = urlopen(req).read()
        try:
            with open(image_full_name,'wb') as file:
                file.write(raw_img)
        except:
            image_full_name = query+str(random.randint(100000))
            with open(image_full_name,'wb') as file:
                file.write(raw_img)
        return image_full_name
    except Exception as e:
        print ("Download failed: {}".format(e))
        return None

def upload_file(file_name,folder_id):
    file2 = drive.CreateFile({'parents': [{'id': folder_id}]})
    file2.SetContentFile(file_name)
    file2.Upload()

这是我的代码到目前为止,它工作得很好,但它是缓慢的(3-6秒每幅图像),所以我想线程它我怎么能做到这一点

图片来自不同的网站,所以我可以下载他们没有太快的要求在网站上


Tags: nameimageidurlimgraw字典download