分类:N05_python
import requests def download_image(image_url, save_path): try: # 发送 GET 请求获取图片内容 response = requests.get(image_url) # 检查请求是否成功 if response.status_code == 200: # 打开文件以二进制写入 with open(save_path, 'wb') as file: file.write(response.content) print(f"Image successfully downloaded: {save_path}") else: print(f"Failed to retrieve image. Status code: {response.status_code}") except Exception as e: print(f"An error occurred: {e}") # 示例用法 image_url = "https://example.com/path/to/image.jpg" # 替换为实际的图片链接 image_url = ["https://example.com/path/to/image.jpg","https://example.com/path/to/image.jpg"] # download_image(image_url, save_path) for i in range(len(image_urls)): save_path = "D:/DownImg/22/" # 图片在本地保存的路径 save_path = save_path+str(i+1)+".png" download_image(image_urls[i], save_path)