def download_images(url, save_folder): # 确保保存目录存在 if not os.path.exists(save_folder): os.makedirs(save_folder)
# 发送HTTP请求获取网页内容 response = requests.get(url) if response.status_code != 200: print(f"Failed to retrieve the webpage. Status code: {response.status_code}") return
# 保存图片 with open(save_path, 'wb') as f: f.write(img_response.content) print(f"Downloaded and saved {img_url} as {save_path}") else: print(f"Failed to download {img_url}. Status code: {img_response.status_code}")
# 读取网址列表文件 def read_urls_from_file(file_path): with open(file_path, 'r') as file: urls = file.readlines() return [url.strip() for url in urls]