帮我爬取一下网站https://bbs.zol.com.cn/dcbbs/d15.html 的图片,并将每一个帖子中的图片,储存到一个文件夹,文件夹使用帖子的名字命名
时间: 2024-03-29 21:40:43 浏览: 15
好的,我可以为您编写一个Python脚本来实现这个功能。您需要安装requests、beautifulsoup4、os和urllib这几个Python库。
以下是代码:
```python
import requests
from bs4 import BeautifulSoup
import os
import urllib.request
url = "https://bbs.zol.com.cn/dcbbs/d15.html"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 获取所有帖子的链接
post_links = []
for a in soup.find_all("a", class_="title-link"):
post_links.append(a["href"])
# 遍历每个帖子
for link in post_links:
response = requests.get(link)
soup = BeautifulSoup(response.text, "html.parser")
# 获取帖子标题
title = soup.find("h3").text.strip()
# 创建文件夹
folder_name = title.replace("/", "_") # 防止标题中包含 /
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# 获取所有图片的链接
img_links = []
for img in soup.find_all("img", class_="lazy"):
img_links.append(img["data-original"])
# 下载图片并保存到文件夹
for i, link in enumerate(img_links):
try:
file_name = os.path.join(folder_name, f"{i}.jpg")
urllib.request.urlretrieve(link, file_name)
except:
pass
```
这段代码会先请求https://bbs.zol.com.cn/dcbbs/d15.html页面,解析出所有帖子的链接,然后遍历每个帖子,获取帖子标题,创建一个以标题命名的文件夹,获取所有图片的链接,下载图片并保存到文件夹中。请注意,这段代码可能会花费一些时间来运行,具体取决于网站的响应速度和您的网络速度。