我需要为我的项目截取网站截图。作为开发语言,我使用Python并使用截图我从PyQt使用Webkit。下面的脚本是用于捕获网站的代码(它已被部分修改,但大部分仍然与webscraping.com中的原始网站相同)。
现在我的问题如下:
大部分时间它没有任何问题,但是,有时会发生以下异常。
QPainter::begin: Paint device returned engine == 0, type: QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setBrush: Painter not active
QPainter::pen: Painter not active
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
我已经将问题跟踪到了图像的创建
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
此行返回的QImage有时是空的 - 我使用.isNull() - QImage方法检查了这个。
根据Qt文档,如果没有足够的内存来分配新的QImage,就会发生这种情况,但我仍然有足够的可用内存。
这个行为是在Windows和Linux上运行时发生的,所以它不应该依赖于一些os依赖的东西,我想。我是Qt和PyQt的新手,所以我希望有人可以帮助我。
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys
import time
# ############################################################# #
# This script is based on the following script: #
# https://webscraping.com/blog/Webpage-screenshots-with-webkit/ #
# ############################################################# #
class Screenshot(QWebView):
_instance = None
@staticmethod
def get_instance():
# TODO: Add a lock around the if including the creation!
if Screenshot._instance is None:
Screenshot._instance = Screenshot()
return Screenshot._instance
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(frame.contentsSize())
# render image
# creating the image. Here it happens that QImage returns a 'Null'-Image
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
# check if there's no image allocated
if image.isNull():
print 'image.isNull() is True'
if image is None:
print 'image is None is True'
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
if __name__ == '__main__':
# a simple way to get the exceptions is to try to create multiple screenshots
sc = Screenshot()
for i in range(0, 25):
sc.capture('http://google.de', str(i) + '.png')
for i in range(25, 50):
sc.capture('http://de.wikipedia.org', str(i) + '.png')
答案 0 :(得分:0)
确定。我已经进一步追踪了这个问题。看起来QWebPages mainFrame的contentsSize有时是(0,0),当它被读取来创建QImage时。
frame = self.page().mainFrame()
self.page().setViewportSize(frame.contentsSize()) # frame.contentsSize() = (0, 0)
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32) # so we're creating here an Image with Width: 0 and Height: 0
所以基本上QImage似乎是Null,因为它创建的大小是(0,0)。
这个问题可以通过检查mainFrames contentsSize是否为(0,0)来解决。如果是(0,0),则需要在QApplication上处理未完成的事件,直到设置新的contentsSize为止。我现在用以下代码执行此操作:
if frame.contentsSize().width() == 0 or frame.contentsSize().height() == 0:
print 'ContentsSize = (w: {}, h: {})'.format(frame.contentsSize().width(), frame.contentsSize().height())
count = 0 # used so we're not starting an infinite loop
while (frame.contentsSize().width() == 0 or frame.contentsSize().height() == 0) and count < 5:
count += 1
self.app.processEvents()
time.sleep(1)