爬蟲下載圖片信息保存到本地文件夾下


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM