python tonardo 下载远程图片到本地

ゝ一世哀愁。 2022-04-06 13:18 410阅读 0赞
  1. image_url = 'https://timgsa.baidu.com/35fa3560.jpg'
  2. img = urllib2.urlopen(image_url)
  3. file_name = int(time.time())
  4. path = str(file_name) + '.png'
  5. root_dir = os.getcwd()
  6. file_path = '%s/static/upload/png/%s' % (root_dir, path)
  7. with open(file_path, 'wb') as localFile:
  8. localFile.write(img.read())
  9. f = open(file_path, 'rb')
  10. contentType = mimetypes.guess_type(file_path)[0]
  11. self.set_header("Content-Type", contentType + ";charset=utf-8")
  12. self.set_header('Content-Disposition', 'attachment; filename=' + 'image_name.png')
  13. self.write(f.read())

发表评论

表情:
评论列表 (有 0 条评论,410人围观)

还没有评论,来说两句吧...

相关阅读