爬虫爬取图片保存到本地
时间: 2024-05-26 22:07:31 浏览: 26
爬虫爬取图片并保存到本地的流程如下:
1. 寻找目标网站,确定要爬取的图片链接所在的页面以及图片链接的规则。
2. 使用 Python 的 requests 库向目标网站发送请求,并使用正则表达式或 Beautiful Soup 等工具解析出所有图片链接。
3. 遍历所有图片链接,使用 requests 库向每个链接发送请求,获取图片的二进制数据。
4. 将二进制数据保存为图片文件,可以使用 Python 的内置库 os 和 shutil,也可以使用第三方库如 PIL 或 cv2。
以下是一个示例代码,假设要从百度图片中搜索“猫”的图片并保存到本地:
```python
import requests
import re
import os
# 目标网站和搜索关键词
url = 'https://image.baidu.com/search/index?tn=baiduimage&word=猫'
# 发送请求并解析出所有图片链接
response = requests.get(url)
html = response.text
img_urls = re.findall(r'"objURL":"(.*?)"', html)
# 遍历所有图片链接,下载并保存图片
if not os.path.exists('images'):
os.mkdir('images')
for i, img_url in enumerate(img_urls):
try:
response = requests.get(img_url, timeout=5)
with open('images/{}.jpg'.format(i), 'wb') as f:
f.write(response.content)
print('成功下载第{}张图片!'.format(i+1))
except Exception as e:
print('下载第{}张图片失败:{}'.format(i+1, e))
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)