你可以使用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和保存的文件路径作为参数调用该函数即可下载图片。