PythonQWebView-带有延迟的屏幕截图
我需要一个python脚本来拍摄网页截图。我有以下运行良好的代码:PythonQWebView-带有延迟的屏幕截图,python,qt4,qwebview,Python,Qt4,Qwebview,我需要一个python脚本来拍摄网页截图。我有以下运行良好的代码: class Screenshot(QWebView): def __init__(self): self.app = QApplication(sys.argv) QWebView.__init__(self) self._loaded = False self.loadFinished.connect(self._loadFinished) def capture(self, url, out
class Screenshot(QWebView):
def __init__(self):
self.app = QApplication(sys.argv)
QWebView.__init__(self)
self._loaded = False
self.loadFinished.connect(self._loadFinished)
def capture(self, url, output_file):
self.load(QUrl(url))
self.wait_load()
# set to webpage size
frame = self.page().mainFrame()
self.page().setViewportSize(QSize(900,1600))
# render image
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
print 'saving', output_file
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
然而,屏幕截图是在页面加载后立即拍摄的,我正在尝试截图的页面有一个JS界面,加载所有内容(大型图形)大约需要20秒。我将如何延迟浏览器
我知道我无法使用time.sleep()执行此操作,因为它会冻结包括web浏览器在内的线程,从而生成相同的图像,因此我知道我需要使用QTimer(),但我尝试了以下操作,但无效:
class Screenshot(QWebView):
def __init__(self):
self.app = QApplication(sys.argv)
QWebView.__init__(self)
def capture(self, url, output_file):
self._file = output_file
# set to webpage size
frame = self.page().mainFrame()
self.page().setViewportSize(QSize(900,1600))
self.timerScreen = QTimer()
self.timerScreen.setInterval(30000)
self.timerScreen.setSingleShot(True)
self.timerScreen.timeout.connect(self.grab)
self.loadFinished.connect(self.timerScreen.start)
self.load(QUrl(url))
def grab(self):
# render image
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
print 'saving', self._file
image.save(self._file)
相反,这会导致对Screenshot.capture()的调用立即返回,并且不会截图
另外,我不能使用app.exec_389;()方法,因为这会导致我的应用程序挂起,因为这是一个更大的headless命令行程序的一部分。这是不可能的
欢迎您的任何意见
干杯你做到了吗?我也有同样的问题你做到了吗?我也有同样的问题