python脚本自动下载图片

from re import I
import requests
import urllib.request
import os
import ssl
import tqdm
ssl._create_default_https_context = ssl._create_unverified_context
# 创建目录文件夹

def download_img(urlPath, outPath):
    # 直接将对应信息写入文件。格式:urllib.request.urlretrieve(url,filename=本地文件地址)
    urllib.request.urlretrieve(urlPath, filename=outPath)

    # urlretrieve执行的过程中,会产生缓存,清除缓存
    urllib.request.urlcleanup()


if __name__ == "__main__":
    os.makedirs('./image/', exist_ok=True)
    os.makedirs('./mask/', exist_ok=True)

    # 构造请求头
    headers={
    
    
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
    }
    # 发送get请求图片url
    for i in tqdm.tqdm(range(730)):
        imgUrl=f'https://zenodo.org/record/4601472/files/img_{
      
      i}.jpg?download=1'
        imgOutPath = f'./image/img_{
      
      i}.jpg'
        maskUrl=f'https://zenodo.org/record/4601472/files/img_{
      
      i}_refined_mask.jpg?download=1'
        maskOutPath = f'./mask/img_{
      
      i}_refined_mask.jpg'
        download_img(imgUrl, imgOutPath)
        download_img(maskUrl, maskOutPath)


猜你喜欢

转载自blog.csdn.net/weixin_42990464/article/details/126464668
今日推荐