共計 521 個字符,預(yù)計需要花費 2 分鐘才能閱讀完成。
在 Python 中,可以使用第三方庫 requests 來獲取圖片,并使用 open() 函數(shù)將其保存到本地。
以下是一個示例代碼:
import requests
# 圖片鏈接
image_url = "https://example.com/image.jpg"
# 發(fā)送 GET 請求獲取圖片數(shù)據(jù)
response = requests.get(image_url)
# 檢查請求是否成功
if response.status_code == 200:
# 從響應(yīng)中獲取圖片數(shù)據(jù)
image_data = response.content
# 保存圖片到本地
with open("image.jpg", "wb") as file:
file.write(image_data)
print(" 圖片保存成功!")
else:
print(" 請求失敗,狀態(tài)碼:", response.status_code)
以上代碼中,首先使用 requests.get() 發(fā)送 GET 請求來獲取圖片數(shù)據(jù),然后檢查請求是否成功(狀態(tài)碼為 200 表示成功)。如果請求成功,將圖片數(shù)據(jù)寫入到名為 image.jpg 的文件中。
請確保已安裝 requests 庫,可以使用以下命令來安裝:
pip install requests
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完