如何使用我已经知道其 URL 地址的 Python 在本地保存图像? [英] How to save an image locally using Python whose URL address I already know?

查看:37
本文介绍了如何使用我已经知道其 URL 地址的 Python 在本地保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Internet 上图像的 URL.

I know the URL of an image on Internet.

例如http://www.digimouth.com/news/media/2011/09/google-logo.jpg,其中包含 Google 的徽标.

e.g. http://www.digimouth.com/news/media/2011/09/google-logo.jpg, which contains the logo of Google.

现在,如何使用 Python 下载此图像,而无需实际在浏览器中打开 URL 并手动保存文件.

Now, how can I download this image using Python without actually opening the URL in a browser and saving the file manually.

推荐答案

Python 2

如果您只想将其另存为文件,这里有一个更直接的方法:

Python 2

Here is a more straightforward way if all you want to do is save it as a file:

import urllib

urllib.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

第二个参数是应该保存文件的本地路径.

The second argument is the local path where the file should be saved.

正如 SergO 建议的,下面的代码应该适用于 Python 3.

As SergO suggested the code below should work with Python 3.

import urllib.request

urllib.request.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

这篇关于如何使用我已经知道其 URL 地址的 Python 在本地保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆