本代码实现使用selenuim库操作浏览器将网页截图并将截图保存成pdf的功能
from selenium import webdriver
from PIL import Image
import io
# 启动Chrome浏览器
driver = webdriver.Chrome()
# 打开网页
driver.get('www.example.cn')
# 创建PDF
pdf_content = io.BytesIO()
images = []
driver.save_screenshot("screenshot.png")
img = Image.open('screenshot.png')
img.save(pdf_content, format='PDF', append_images=[img])
# 关闭浏览器
driver.quit()
# 将内存中的 PDF 内容写入文件
with open('output.pdf', 'wb') as f:
f.write(pdf_content.getvalue())