码迷,mamicode.com
首页 > 编程语言 > 详细

python 下载图片

时间:2020-03-26 21:31:57      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:exception   img   safari   apple   pat   nload   gecko   try   print   

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

    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).content

        with open(save_img_path ,wb ) as f:
            f.write(img)
        # f = open(SAVE_ROOT_PATH+‘download_img\\‘+ str(imgName)+str(file_suffix), ‘wb‘)
        # f.write((urllib.request.urlopen(imgUrl,timeout=60)).read())
        print(imgUrl,下载成功)
        # f.close()
        # return ROOT_URL+‘tangfeng/public/download_img/‘+ str(imgName)+str(file_suffix)
        return save_img_path
        # return True
    except Exception as e:
        print(imgUrl,"下载图片错误XXXX",e)
        try:
            os.remove(save_img_path) 
        except Exception as e:
            pass
        return False

 

python 下载图片

标签:exception   img   safari   apple   pat   nload   gecko   try   print   

原文地址:https://www.cnblogs.com/xiondun/p/12577173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!