python 下载图片
Posted xiondun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 下载图片相关的知识,希望对你有一定的参考价值。
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 下载图片的主要内容,如果未能解决你的问题,请参考以下文章