你可以使用Python中的requests庫來下載圖片。以下是一個簡單的示例代碼:
import requests
def download_image(url, file_path):
response = requests.get(url)
if response.status_code == 200:
with open(file_path, 'wb') as file:
file.write(response.content)
print(f"Image downloaded successfully to {file_path}")
else:
print("Failed to download image")
# 用法示例
url = "https://example.com/image.jpg"
file_path = "image.jpg"
download_image(url, file_path)
在上面的示例中,我們定義了一個download_image
函數來下載圖片。你只需要傳入圖片的URL和保存的文件路徑作為參數調用該函數即可下載圖片。