使用PyQt6/QWebEngineView保存网页截图始终为空的问题求助
PyQt6/QWebEngineView保存网页截图始终为空的问题求助
我尝试用QtWebEngineView保存网页截图,但生成的图片总是空的。
以下是我的代码:
import sys import time from PyQt6.QtCore import * from PyQt6.QtGui import * from PyQt6.QtWidgets import QApplication, QWidget from PyQt6.QtWebEngineWidgets import QWebEngineView class Screenshot(QWebEngineView): def __init__(self): self.app = QApplication(sys.argv) QWebEngineView.__init__(self) self._loaded = False self.loadFinished.connect(self._loadFinished) def capture(self, url, output_file): self.resize(QSize(1024, 768)) self.load(QUrl(url)) self.wait_load() image = QImage(self.size(), QImage.Format.Format_ARGB32) painter = QPainter(image) self.render(painter) painter.end() image.save(output_file) def wait_load(self, delay=0): # process app events until page loaded while not self._loaded: self.app.processEvents() time.sleep(delay) self._loaded = False def _loadFinished(self, result): self._loaded = True s = Screenshot() s.capture("https://www.google.com", "screenshot.png")
我试过让QTWebEngineView在应用窗口中渲染的变体代码,那部分是正常工作的。wait_load函数也确保了在开始绘制前网页已经加载完成,但不知为何渲染出来的图片始终是全白的png,而不是网页内容。
编辑:申请重新开放问题,因为“重复”问题中的解决方案似乎无法解决这个案例。
备注:内容来源于stack exchange,提问作者kontur
相关产品推荐
相关产品推荐

