网页中图片在canvas中,不知道图片链接。用下面代码可以尝试下载。
[Python] 纯文本查看 复制代码 from selenium import webdriver
from selenium.webdriver.common.by import By
import base64
import io
from PIL import Image
import os
import time
# 初始化WebDriver
driver = webdriver.Edge() # 或者使用其他浏览器,如Firefox
# 设置下载图片的路径
download_path = 'd:/a'
if not os.path.exists(download_path):
os.makedirs(download_path)
# 打开网页
url = 'https://www.docin.com/p-xxxxxxxxx.html' # 替换为包含Canvas元素的网页URL
driver.get(url)
# 等待页面加载完成(可选,根据页面加载情况调整)
a = input('等待加载完成:') # 或者使用WebDriverWait和expected_conditions来等待特定条件
# 获取页面中所有的Canvas元素
canvases = driver.find_elements(By.TAG_NAME, 'canvas')
# 遍历每个Canvas元素并下载图片
for index, canvas in enumerate(canvases):
# 执行JavaScript脚本获取Canvas图片数据
canvas_data = driver.execute_script("""
return arguments[0].toDataURL('image/png').replace('image/png', 'image/octet-stream');
""", canvas)
# 将Base64编码的图片数据转换为二进制数据
image_data = base64.b64decode(canvas_data.split(',')[1])
# 将二进制数据转换为图片文件并保存到本地
image_name = f'canvas_image_{str(index+1).zfill(3)}.png'
image_path = os.path.join(download_path, image_name)
with open(image_path, 'wb') as handler:
handler.write(image_data)
print(f'Downloaded: {image_path}')
# 关闭WebDriver
driver.quit()
|