import os import requests from bs4 import BeautifulSoup 设置要下载的页面 URL…
Jump to turn (6)
- 1import os import requests from bs4 import BeautifulSoup 设置要下载的页面 URL 和图片存储的目录(可以
- 2发生异常: NameError name 'url' is not defined File "F:\maomaochong\caterpillar\brick
- 3需要翻页。请帮我完善代码
- 4发生异常: AttributeError 'NoneType' object has no attribute 'text' File "F:\maomaoch
- 5发生异常: Exception 页面结构发生了变化,请检查是否需要更新代码! File "F:\maomaochong\caterpillar\brick bi
- 6我觉得不纠结了,我们直接下载https://brickvisual.com/和的所有图片,当然小于100kb的图片不要
- User
import os import requests from bs4 import BeautifulSoup
设置要下载的页面 URL 和图片存储的目录(可以修改为你自己的目录)
image_dir = 'F:/BC' url_base = 'https://brickvisual.com/works/#&gid=1&pid='
创建图片存储目录
if not os.path.exists(image_dir): os.makedirs(image_dir)
发送 GET 请求获取页面内容
response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser')
遍历页面中的所有图片元素
for img in soup.find_all('a', {'class': 'portfolio-item-img-link'}): # 获取源文件的 URL source_url = img['href']
code# 发送 GET 请求获取源文件内容 source_response = requests.get(source_url) # 获取源文件的文件名,可以根据需要修改成自己想要的文件名 source_filename = source_url.split('/')[-1] # 拼接出源文件的完整路径,然后保存源文件 source_path = os.path.join(image_dir, source_filename) with open(source_path, 'wb') as f: f.write(source_response.content) print(f"已保存源文件: {source_filename}") - Assistant