优化截图,但是会残留大量的文件
This commit is contained in:
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user