用Python爬取包图网图片代码
时间: 2024-05-05 18:15:35 浏览: 11
以下是使用Python爬取包图网图片的代码:
```python
import requests
import os
from bs4 import BeautifulSoup
# 设置图片保存路径
save_path = './images'
if not os.path.exists(save_path):
os.makedirs(save_path)
# 包图网首页URL
url = 'http://ibaotu.com/'
# 获取网页HTML代码
html = requests.get(url).text
# 解析网页内容
soup = BeautifulSoup(html, 'html.parser')
# 获取今日推荐区域的所有图片
images = soup.find('div', {'class': 'today-recommend'}).find_all('img')
# 下载图片
for image in images:
image_url = image['src']
image_name = image_url.split('/')[-1]
save_file = os.path.join(save_path, image_name)
with open(save_file, 'wb') as f:
f.write(requests.get(image_url).content)
print('下载完成:', image_name)
```
该代码会从包图网的首页获取今日推荐区域的所有图片,并保存到指定的文件夹中。请注意,此代码仅用于学习和研究,不得用于商业用途。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)