1. 程式人生 > >python tonardo 下載遠端圖片到本地

python tonardo 下載遠端圖片到本地

 	     image_url = 'https://timgsa.baidu.com/35fa3560.jpg'
            img = urllib2.urlopen(image_url)
            file_name = int(time.time())
            path = str(file_name) + '.png'
            root_dir = os.getcwd()
            file_path = '%s/static/upload/png/%s' % (root_dir, path)
            with open(file_path, 'wb') as localFile:
                localFile.write(img.read())
                f = open(file_path, 'rb')
                contentType = mimetypes.guess_type(file_path)[0]
                self.set_header("Content-Type", contentType + ";charset=utf-8")
                self.set_header('Content-Disposition', 'attachment; filename=' + 'image_name.png')
                self.write(f.read())