要使用Python爬取图片,可以使用Python的第三方库requests和BeautifulSoup来实现。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 定义目标网页的URL
url = 'https://www.example.com'
# 发起请求并获取网页内容
response = requests.get(url)
html_content = response.content
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, 'html.parser')
# 找到所有的<img>标签
img_tags = soup.find_all('img')
# 遍历所有的<img>标签,获取图片链接并保存图片
for img_tag in img_tags:
img_url = img_tag['src']
img_name = img_url.split('/')[-1]
# 下载图片并保存
img_data = requests.get(img_url).content
with open(img_name, 'wb') as f:
f.write(img_data)
print(f"图片 {img_name} 下载完成")
```
请注意,这只是一个简单的示例代码。实际爬取图片时,可能需要处理网页链接的相对路径、排除广告图片等情况,还需遵守网站的爬虫规则。