簡體   English   中英

如何使用url下載帶有名稱的圖像?

[英]How to download image with name using url?

我想使用python使用圖像url下載圖片。 我怎樣才能下載它? 我可以嘗試在 google 上提供的所有訂單,但沒有一個代碼不適用於這種類型的 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)

嘗試改變

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM