python简单爬虫爬取图片
时间: 2024-10-29 15:20:07 浏览: 14
Python爬虫爬取图片通常使用requests库获取网页内容,然后解析HTML结构找到图片链接,最后使用BeautifulSoup、PIL等库来下载图片。下面是一个简单的步骤说明:
1. **导入必要的库**:
```python
import requests
from bs4 import BeautifulSoup
import os
```
2. **发送GET请求获取网页源码**:
```python
url = "目标图片所在的网页URL"
response = requests.get(url)
```
3. **解析HTML,定位图片链接**:
```python
soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all('img') # 找到所有的img标签
img_links = [img['src'] for img in img_tags] # 提取src属性作为图片地址
```
4. **创建目录并下载图片**:
```python
if not os.path.exists("保存图片的本地路径"):
os.makedirs("保存图片的本地路径")
for link in img_links:
image_response = requests.get(link)
with open(os.path.join("保存图片的本地路径", link.split("/")[-1]), "wb") as f:
f.write(image_response.content)
```
5. **处理可能出现的异常**:
```python
try:
# 上述代码
except Exception as e:
print(f"下载图片时遇到错误:{e}")
```
阅读全文