标签: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
标签:exception img safari apple pat nload gecko try print
原文地址:https://www.cnblogs.com/xiondun/p/12577173.html