利用snapshot的方法将html渲染为JPG

背景

生成或者爬取大量的html数据后,提升大模型的OCR能力需要将数据转化为JPG文件。

最开始尝试用 selenium.webdriver.chrome.options来进行快照,但是不仅资源占用不释放,而且html的动画也会影响效果。

后面找到了snapshot_selenium这个库,使用 pyechartssnapshot_selenium 来生成 HTML 文件的截图。

代码原理

功能实现

  • 无头浏览器设置:配置了 Chrome 无头模式和其他优化设置。
  • 文件路径处理:定义了 HTML 文件的目录,并创建了截图和错误文件夹。
  • 错误处理:达到最大重试次数后,文件被移动到错误文件夹

代码实现

from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
import os
import shutil
import time
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from tqdm import tqdm

# 设置无头浏览器模式并优化Chrome设置
def get_chrome_options():
    chrome_options = ChromeOptions()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.page_load_strategy = 'normal'  # 增加页面加载策略
    return chrome_options


# 文件夹路径
folder_path = r'error_files\error_files'

# 获取所有HTML文件
html_files = [f for f in os.listdir(folder_path) if f.endswith('.html')]

# 定义截图和错误文件夹路径
screenshot_folder = os.path.join(folder_path, 'snapshot')
error_folder = os.path.join(folder_path, 'error_files')

# 创建文件夹
os.makedirs(screenshot_folder, exist_ok=True)
os.makedirs(error_folder, exist_ok=True)

# 最大重试次数
max_retries = 1

# 处理每个HTML文件
for html_file in tqdm(html_files):
    html_file_path = os.path.join(folder_path, html_file)
    screenshot_path = os.path.join(screenshot_folder, os.path.splitext(html_file)[0] + ".png")

    for attempt in range(max_retries):
        try:
            # 启动Chrome浏览器
            service = ChromeService(ChromeDriverManager().install())
            options = get_chrome_options()
            browser = Chrome(service=service, options=options)

            # 生成截图
            make_snapshot(snapshot, html_file_path, screenshot_path, driver=browser)

            # 关闭浏览器
            browser.quit()

            # 显式释放资源
            del browser
            del service

            print(f"Processed: {html_file},success")
            break  # 成功处理后跳出重试循环
        except Exception as e:
            print(f"Error processing {html_file} on attempt {attempt + 1}/{max_retries}: {e}")
            if attempt == max_retries - 1:
                # 将出错文件移动到错误文件夹
                error_file_path = os.path.join(error_folder, html_file)
                shutil.move(html_file_path, error_file_path)
                print(f"Failed to process {html_file} after {max_retries} attempts. Copied to error_files.")

        # 缓解CPU负担
        time.sleep(0.1)

 输出结果

大多数html能完美渲染成JPG图片,但是仍然存在数据缺失的数据。

后续处理

有些是不能正常渲染的数据,后续需要进行数据清洗,进行筛选。

数据处理-删除无内容的图片_将数据集中全部为背景或脑部组织较少的无效图像删除-CSDN博客

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gatinaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值