def save_img(self,url, file_name, file_path='drug_specification'):
# 保存圖片到磁盤文件夾 file_path中,默認為當前腳本運行目錄下的 book\img文件夾
try:
if not os.path.exists(file_path):
print '文件夾', file_path, '不存在,重新建立'
# os.mkdir(file_path)
os.makedirs(file_path)
# 獲得圖片后綴
file_suffix = os.path.splitext(url)[1]
# 拼接圖片名(包含路徑)
filename = '{}{}{}{}'.format(file_path, os.sep, file_name['drug_name'].encode('utf-8'), file_suffix)
# 下載圖片,並保存到文件夾中
urllib.urlretrieve(url, filename=filename)
except IOError as e:
print '文件操作失敗', e
except Exception as e:
log = traceback.format_exc()
print '錯誤 :', log