优化截图,但是会残留大量的文件

This commit is contained in:
liuwei
2025-05-27 17:29:53 +08:00
parent 86b257ce20
commit d326467a04
2 changed files with 15 additions and 8 deletions

View File

@@ -211,12 +211,17 @@ async def html_to_image(html_file, output_image):
browser = await p.chromium.launch(executable_path=browser_path)
# 业务逻辑不变
page = await browser.new_page()
await page.goto(f'file://{os.path.abspath(html_file)}')
await page.set_viewport_size({"width": 750, "height": 800})
await page.wait_for_timeout(500)
await page.screenshot(path=output_image, full_page=True)
await browser.close()
try:
page = await browser.new_page()
await page.goto(f'file://{os.path.abspath(html_file)}')
await page.set_viewport_size({"width": 750, "height": 800})
await page.wait_for_timeout(500)
await page.screenshot(path=output_image, full_page=True)
except Exception as e:
print(f"截图失败: {e}")
finally:
await page.close()
await browser.close()
except Exception as e:
print(f"浏览器操作失败: {e}")