【发布时间】:2020-11-16 20:58:31
【问题描述】:
我想使用python下载图片url。我怎样才能下载它?我可以尝试谷歌上所有可用的订单,但没有一个代码不适用于这种类型的网址。
我试过这段代码:
import requests # to get image from the web
import shutil # to save it locally
## Set up the image URL and filename
image_url = "https://graph.facebook.com/264520706917918/picture"
filename = image_url.split("/")[-1]
# Open the url image, set stream to True, this will return the stream content.
r = requests.get(image_url, stream = True)
# Check if the image was retrieved successfully
if r.status_code == 200:
# Set decode_content value to True, otherwise the downloaded image file's size will be zero.
r.raw.decode_content = True
# Open a local file with wb ( write binary ) permission.
with open(filename,'wb') as f:
shutil.copyfileobj(r.raw, f)
print('Image sucessfully Downloaded: ',filename)
else:
print('Image Couldn\'t be retreived')
还有这些代码:
# import wget
# # Set up the image URL
# image_url = "https://graph.facebook.com/264520706917918/picture"
# # Use wget download method to download specified image url.
# image_filename = wget.download(image_url)
# print('Image Successfully Downloaded: ', image_filename)
【问题讨论】:
-
请附上您尝试过的代码。
-
我已经尝试过了,但无法正常工作,它只是下载格式未知的图像,我无法打开它
标签: python python-3.x image python-requests