import os import requests from bs4 import BeautifulSoup 设置要下载的页面 URL…
- User
import os import requests from bs4 import BeautifulSoup
设置要下载的页面 URL 和图片存储的目录(可以修改为你自己的目录)
image_dir = 'F:/BC' url = 'https://brickvisual.com/'
创建图片存储目录
if not os.path.exists(image_dir): os.makedirs(image_dir)
下载页面中的所有图片
response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser')
for img in soup.find_all('img'): # 获取图片的 URL img_url = img['src']
code# 发送 GET 请求获取图片内容 img_response = requests.get(img_url) # 过滤掉小于 100KB 的图片 if len(img_response.content) < 100 * 1024: continue # 获取图片的文件名 img_filename = img_url.split('/')[-1] # 拼接出图片的完整路径,然后保存图片 img_path = os.path.join(image_dir, img_filename) with open(img_path, 'wb') as f: f.write(img_response.content) print(f"已保存图片: {img_filename}“)下载其它页面中的图片
for link in soup.find_all('a'): # 获取链接的 URL link_url = link['href']
code# 如果链接不是以 https://brickvisual.com/ 开头,则跳过 if not link_url.startswith('https://brickvisual.com/'): continue print(f"正在处理链接: {link_url}…”) link_response = requests.get(link_url) link_soup = BeautifulSoup(link_response.text, 'html.parser') for img in link_soup.find_all('img'): # 获取图片的 URL img_url = img['src'] # 发送 GET 请求获取图片内容 img_response = requests.get(img_url) # 过滤掉小于 100KB 的图片 if len(img_response.content) < 100 * 1024: continue # 获取图片的文件名 img_filename = img_url.split('/')[-1] # 拼接出图片的完整路径,然后保存图片 img_path = os.path.join(image_dir, img_filename) with open(img_path, 'wb') as f: f.write(img_response.content) print(f"已保存图片: {img_filename}")print(“下载完成!”) Traceback (most recent call last): File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\ASUS\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in .py", line 39, in cli.main() File "c:\Users\ASUS.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main run() File "c:\Users\ASUS.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file runpy.run_path(target, run_name="") File "c:\Users\ASUS.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 320, in run_path code, fname = _get_code_from_file(run_name, path_name) File "c:\Users\ASUS.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py", line 294, in _get_code_from_file code = compile(f.read(), fname, 'exec') File "F:\maomaochong\caterpillar\brick big image.py", line 35 print(f"已保存图片: {img_filename}“) ^
- Assistant
There is a syntax error in the print statement on line 35. The closing quotation mark is a different character than the opening quotation mark, causing a syntax error. Replace the closing quotation mark with the correct character and the error should be resolved.
Corrected code:
python - User
No connection adapters were found for 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAADCAQAAAD41K0JAAAADUlEQVR42mNkYGAEIwAAGwAE61CSsQAAAABJRU5ErkJggg==' File "F:\maomaochong\caterpillar\brick big image.py", line 22, in img_response = requests.get(img_url) requests.exceptions.InvalidSchema: No connection adapters were found for 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAADCAQAAAD41K0JAAAADUlEQVR42mNkYGAEIwAAGwAE61CSsQAAAABJRU 发生了这个错误
- Assistant
This error occurs because the image URL is a base64 encoded data URI that begins with "data:image/". The requests library cannot handle these types of data URIs.
To fix this error, you can skip any image URLs that begin with "data:image/". Modify the code within the for loop that downloads images as follows:
pythonfor img in soup.find_all('img'): # 获取图片的 URL img_url = img['src'] # 如果图片 URL 是 data URI,则跳过 if img_url.startswith('data:image/'): continue # 发送 GET 请求获取图片内容 img_response = requests.get(img_url) # 过滤掉小于 100KB 的图片 if len(img_response.content) < 100 * 1024: continue # 获取图片的文件名 img_filename = img_url.split('/')[-1] # 拼接出图片的完整路径,然后保存图片 img_path = os.path.join(image_dir, img_filename) with open(img_path, 'wb') as f: f.write(img_response.content) print(f"已保存图片: {img_filename}") - User
可以把完整代码发上来吗
- Assistant
Certainly! Here is the modified code to skip images with URLs beginning with "data:image/".
python - User
发生异常: MissingSchema Invalid URL '/wp-content/uploads/2022/02/272878143_487477122739422_3708429531263917196_n-1280x1082.jpg': No scheme supplied. Perhaps you meant https:///wp-content/uploads/2022/02/272878143_487477122739422_3708429531263917196_n-1280x1082.jpg? File "F:\maomaochong\caterpillar\brick big image.py", line 26, in img_response = requests.get(img_url) requests.exceptions.MissingSchema: Invalid URL '/wp-content/uploads/2022/02/272878143_487477122739422_3708429531263917196_n-1280x1082.jpg': No scheme supplied. Perhaps you meant https:///wp-content/uploads/2022/02/272878143_487477122739422_3708429531263917196_n-1280x1082.jpg? 你好,又发生了异常
- Assistant
- User
发生异常: OSError [Errno 22] Invalid argument: 'F:/BC\ac1399c5-2dbb-4e06-a03f-b0cfca0951e5.gif?ClientID=vimeo-core-prod&Date=1683217513&Signature=5fe3330cd05e23dec7091fb53e0cb857e5d2ffc4' File "F:\maomaochong\caterpillar\brick big image.py", line 83, in with open(img_path, 'wb') as f: OSError: [Errno 22] Invalid argument: 'F:/BC\ac1399c5-2dbb-4e06-a03f-b0cfca0951e5.gif?ClientID=vimeo-core-prod&Date=1683217513&Signature=5fe3330cd05e23dec7091fb53e0cb857e5d2ffc4' 还是有异常
- Assistant
This error occurs because the image filename contains characters that are not allowed in filenames for Windows systems. In this case, the invalid character is the question mark ("?") that is part of the Vimeo URL.
To fix this error, you can remove the invalid characters from the filenames. One way to do this is to extract only