PyQt 等待页面加载

发布于 2024-10-17 06:11:37 字数 1064 浏览 8 评论 0原文

我想在完全加载时将页面内容保存到图像中,但有时我得到的输出光栅未完全渲染。

代码:

import sys
import signal
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
def onLoadFinished(result):
    if not result:
        print "Request failed"
        sys.exit(1)
    webpage.setViewportSize(webpage.mainFrame().contentsSize())
    image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    webpage.mainFrame().render(painter)
    painter.end()
    if os.path.exists("output.png"):
        os.remove("output.png")
    image.save("output.png")
    sys.exit(0) # quit this application

webpage.mainFrame().load(QUrl("file:///page.html"))
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
sys.exit(app.exec_())

页面正在使用JavaScript(onload函数)获取谷歌地图(640x640px)。

图片:http://i56.tinypic.com/15ojg3s.png

I want to save a page content to an image when it is fully loaded but sometimes i am getting output raster not rendered completely.

Code:

import sys
import signal
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
def onLoadFinished(result):
    if not result:
        print "Request failed"
        sys.exit(1)
    webpage.setViewportSize(webpage.mainFrame().contentsSize())
    image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    webpage.mainFrame().render(painter)
    painter.end()
    if os.path.exists("output.png"):
        os.remove("output.png")
    image.save("output.png")
    sys.exit(0) # quit this application

webpage.mainFrame().load(QUrl("file:///page.html"))
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
sys.exit(app.exec_())

Page is using JavaScript (onload function) to acquire google map (640x640px) .

Image: http://i56.tinypic.com/15ojg3s.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我还不会笑 2024-10-24 06:11:37

我不确定这是否可能。对于静态网站,这可能可行,但谷歌地图将动态加载图块,我怀疑它会发出可用的“我完成了”信号。

但你似乎只想要一张谷歌地图的图像?你看过他们的API吗?它们允许您通过构建网址来生成静态地图

示例

http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap &markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318 &markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false

示例地图

I'm not sure if this even possible. For a static website this could probably work, but Google Maps will load tiles dynamically, and I'm in doubt it will emit a usuable "I'm done" signal.

But it seems you only want an image of a Google map? Have you looked at their API? They allow you to generate static maps, just by building a URL.

Example

http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap &markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318 &markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false

Example map

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文