fanjp666888

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

 

分类:

技术点:

相关文章:

  • 2021-08-12
  • 2022-01-27
  • 2021-11-30
  • 2021-11-26
  • 2021-11-23
  • 2021-12-14
  • 2021-10-24
  • 2021-06-19
猜你喜欢
  • 2021-12-03
  • 2021-09-22
  • 2021-04-10
  • 2021-07-21
  • 2021-12-09
  • 2021-11-15
  • 2021-10-26
相关资源
相似解决方案