本文目录导读:

我来介绍几种在Python中下载网络图片的常用方法:
方法1:使用 requests 库(推荐)
import requests
import os
def download_image(url, save_path):
try:
# 发送GET请求获取图片
response = requests.get(url, stream=True)
response.raise_for_status() # 检查请求是否成功
# 保存图片
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"图片已保存到: {save_path}")
return True
except Exception as e:
print(f"下载失败: {e}")
return False
# 使用示例
url = "https://example.com/image.jpg"
save_path = "downloaded_image.jpg"
download_image(url, save_path)
方法2:使用 urllib 库(Python内置)
import urllib.request
import os
def download_image_urllib(url, save_path):
try:
# 下载图片
urllib.request.urlretrieve(url, save_path)
print(f"图片已保存到: {save_path}")
return True
except Exception as e:
print(f"下载失败: {e}")
return False
# 使用示例
url = "https://example.com/image.jpg"
save_path = "downloaded_image.jpg"
download_image_urllib(url, save_path)
方法3:批量下载图片
import requests
import os
from urllib.parse import urlparse
import time
def batch_download_images(image_urls, save_dir="images"):
"""
批量下载图片
:param image_urls: 图片URL列表
:param save_dir: 保存目录
"""
# 创建保存目录
if not os.path.exists(save_dir):
os.makedirs(save_dir)
success_count = 0
for i, url in enumerate(image_urls):
try:
# 从URL提取文件名
filename = os.path.basename(urlparse(url).path)
if not filename:
filename = f"image_{i+1}.jpg"
save_path = os.path.join(save_dir, filename)
# 下载图片
response = requests.get(url, timeout=10)
response.raise_for_status()
# 保存图片
with open(save_path, 'wb') as f:
f.write(response.content)
print(f"[{i+1}/{len(image_urls)}] 下载成功: {filename}")
success_count += 1
# 添加延迟,避免请求过快
time.sleep(0.5)
except Exception as e:
print(f"[{i+1}/{len(image_urls)}] 下载失败: {url} - {e}")
print(f"\n下载完成! 成功: {success_count}/{len(image_urls)}")
# 使用示例
image_urls = [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg",
"https://example.com/image3.jpg"
]
batch_download_images(image_urls)
方法4:带进度条的下载
import requests
from tqdm import tqdm
def download_with_progress(url, save_path):
try:
response = requests.get(url, stream=True)
response.raise_for_status()
# 获取文件大小
total_size = int(response.headers.get('content-length', 0))
# 创建进度条
with open(save_path, 'wb') as f:
with tqdm(total=total_size, unit='B', unit_scale=True, desc="下载进度") as pbar:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
pbar.update(len(chunk))
print(f"下载完成: {save_path}")
except Exception as e:
print(f"下载失败: {e}")
# 需要先安装 tqdm: pip install tqdm
url = "https://example.com/large_image.jpg"
download_with_progress(url, "large_image.jpg")
安装依赖
# 安装requests库(如果使用requests方法) pip install requests # 安装tqdm(如果需要进度条) pip install tqdm
注意事项
-
请求头设置:有些网站需要设置User-Agent等headers
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' } response = requests.get(url, headers=headers) -
错误处理:始终添加异常处理
-
遵守robots.txt:尊重网站的爬虫规则
-
频率控制:批量下载时添加适当延迟
-
版权问题:确保有权限下载和使用图片
选择哪种方法取决于你的具体需求,requests库功能更强大,urllib则不需要额外安装。