我想使用QtWebKit(pyQt)将网页保存为PNG图像。
我在互联网上找到了以下代码,但作为输出我得到白色图像:
import sys
import signal
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage
def onLoadFinished(result):
if not result:
print "Request failed"
sys.exit(1)
# Set the size of the (virtual) browser window
webpage.setViewportSize(QSize(640,480))
# Paint this frame into an image
image = QImage(QSize(640,480), QImage.Format_ARGB32)
paint = QPainter(image)
print webpage.mainFrame().toHtml()
webpage.mainFrame().render(paint)
paint.end()
image.save("output.png")
sys.exit(0)
app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl("http://maps.googleapis.com/maps/api/streetview?size=640x480&location=50.073723,14.43037&pitch=-0.760&sensor=false&heading=-169.73968469794528"))
sys.exit(app.exec_())
它可以正确保存我测试过的其他网页,但无法处理Google街景。
print webpage.mainFrame().toHtml()
显示已加载正确的网页,但不知何故它不会呈现它......你知道为什么吗?
我使用:PyQt4-4.10.2-gpl-Py2.7-Qt4.8.4-x64