• python 下载图片


    
    
    def mkdir(path):
    path = path.strip()
    path = path.rstrip("\")
    isExists = os.path.exists(path)
    if not isExists:
    os.makedirs(path)
    return True
    else:
    return False


    def down_load_img(imgUrl, imgName=''):
    if not imgName:
    imgName = int(time.time()) + random.randint(0, 999999)
    file_suffix = os.path.splitext(imgUrl)[1]
    file_suffix = file_suffix if file_suffix else '.jpg'

    save_folder = time.strftime(r"%Y%m%d", time.localtime(int(time.time())))
    SAVE_ROOT_PATH = '/expressell/public/uploads/products/' + save_folder

    mkdir(SAVE_ROOT_PATH)
    save_img_path = SAVE_ROOT_PATH + '/' + str(imgName) + str(file_suffix)
    try:
    header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
    }
    img = requests.get(imgUrl, headers=header, timeout=timeout)
    if img.status_code == 200:
    with open(save_img_path, 'wb') as f:
    f.write(img.content)
    print(imgUrl, '下载成功')
    return save_img_path
    else:
    print(imgUrl, '下载失败,', img.status_code)
    except Exception as e:
    print(imgUrl, "下载图片错误XXXX", e)
    try:
    os.remove(save_img_path)
    except Exception as e:
    pass
    return False
  • 相关阅读:
    B. Pasha and Phone
    GCD(关于容斥原理)
    二分乘法
    Lucky7(容斥原理)
    E. Devu and Flowers
    最大公约数最小公倍数
    leetcode每日刷题计划--day55
    Titanic--kaggle竞赛入门-生存预测
    做题记录--day54
    机器学习上机作业1记录 && 相关知识点复习
  • 原文地址:https://www.cnblogs.com/xiondun/p/12577173.html
Copyright © 2020-2023  润新知