【问题标题】:How to download image with name using url?如何使用 url 下载带有名称的图像?
【发布时间】: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)

【问题讨论】:

  • 请附上您尝试过的代码。
  • 可能是一个奇怪的问题,但你试过谷歌搜索吗?我找到了thisthisthisthis,我可以继续前进
  • 我已经尝试过了,但无法正常工作,它只是下载格式未知的图像,我无法打开它

标签: python python-3.x image python-requests


【解决方案1】:

尝试改变

r = requests.get(image_url, stream = True)

r = requests.get(image_url, stream = True).raw

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多