python爬虫爬取图片
时间: 2023-11-08 14:56:32 浏览: 109
Python爬虫项目之爬取校花图片.zip
以下是Python爬虫爬取图片的基本步骤:
1. 导入必要的模块
```python
import requests
from bs4 import BeautifulSoup
import os
```
2. 获取网页源代码
```python
url = 'https://www.example.com' # 要爬取图片的网址
response = requests.get(url)
html = response.text
```
3. 解析HTML代码
```python
soup = BeautifulSoup(html, 'html.parser')
```
4. 查找图片链接
```python
img_tags = soup.find_all('img')
img_urls = [img['src'] for img in img_tags]
```
5. 下载图片
```python
for url in img_urls:
response = requests.get(url)
filename = os.path.basename(url) # 获取文件名
with open(filename, 'wb') as f:
f.write(response.content)
```
完整的Python爬虫爬取图片的代码如下:
```python
import requests
from bs4 import BeautifulSoup
import os
url = 'https://www.example.com'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
img_tags = soup.find_all('img')
img_urls = [img['src'] for img in img_tags]
for url in img_urls:
response = requests.get(url)
filename = os.path.basename(url)
with open(filename, 'wb') as f:
f.write(response.content)
```
注意:爬取网站的图片需要遵守相关法律法规,不得侵犯他人权益和隐私。
阅读全文