python抓取指定页数网页图片代码
时间: 2024-12-09 09:18:50 浏览: 11
在Python中,我们可以使用BeautifulSoup库来解析HTML,PIL库来处理图像,以及requests库来发送HTTP请求。以下是一个简单的示例,展示如何抓取指定页数的网页图片:
```python
import requests
from bs4 import BeautifulSoup
import os
def get_images(url, page_num, save_dir):
# 请求指定页面
response = requests.get(url + f'?page={page_num}')
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 查找所有的img标签
img_tags = soup.find_all('img', src=True)
for img_tag in img_tags:
try:
# 获取图片URL
img_url = img_tag['src']
# 拼接完整的图片地址
full_img_url = url + img_url
# 下载图片并保存到指定目录
response = requests.get(full_img_url, stream=True)
filename = os.path.join(save_dir, img_url.split('/')[-1])
with open(filename, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
print(f'Saved image {filename}')
except Exception as e:
print(f'Error processing image: {e}')
# 设置目标网站、页面范围和保存路径
base_url = 'https://example.com'
start_page = 1
end_page = 5
save_directory = './images'
for i in range(start_page, end_page + 1):
get_images(base_url, i, save_directory)
阅读全文