[英]Take a screenshot of an iFrame using pyside
I have an issue taking screenshots of a Google-translated page. 我在拍摄Google翻译页面的屏幕截图时遇到问题。 The below script is working fine (takes a screenshot) without Google translate: 下面的脚本在没有 Google翻译的情况下可以正常工作(截图):
class Render(QWebPage):
def __init__(self, url):
self.web_page = QWebPage()
self.finished = False
s = self.web_page.settings()
s.setAttribute(QWebSettings.JavascriptCanOpenWindows, False)
s.setAttribute(QWebSettings.PluginsEnabled, True)
self.web_page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
self.web_page.loadFinished.connect(self._loadFinished)
self.web_page.mainFrame().load(QUrl(url))
def _loadFinished(self, result):
frame = self.web_page.mainFrame()
size = frame.contentsSize()
size.setWidth(1000)
self.web_page.setViewportSize(size)
image = QImage(self.web_page.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
self.filepath="screenshot_name.jpg"
image.save(self.filepath)
self.finished = True
def run(url):
app=QApplication.instance()
if not app:
app = QApplication(sys.argv)
r = Render(url)
while not r.finished:
app.processEvents()
time.sleep(0.01)
return r.filepath
news_url="http://arabic-media.com/arabicnews.htm"
news_url_google_translate = "http://translate.google.com/translate?hl=en&sl=auto&tl=en&u="+news_url
run(news_url_google_translate)
But I need to take a screenshot of the translated page. 但是我需要对翻译页面进行截图。 Google translated page creates iframes. Google翻译的页面会创建iframe。 So I used the following to take a screenshot of the inner frame (name=c): 因此,我使用以下内容拍摄了内部框架(名称= c)的屏幕截图:
frame = self.web_page.mainFrame().childFrames()[0]
Unfortunately, my script is still taking black screenshots. 不幸的是,我的脚本仍在拍摄黑色屏幕截图。
See if this example works for you. 查看此示例是否适合您。 The finished
signal of the network manager is connected to a slot that checks the url that has finished downloading, and triggers the screenshot if it finds urlRequest
in it: 网络管理器的finished
信号连接到用于检查已完成下载的url的插槽,并在其中找到urlRequest
触发屏幕截图:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtCore, QtGui, QtWebKit, QtNetwork
urlTranslate = "http://translate.google.com/translate?hl=en&sl=auto&tl=en&u=http://arabic-media.com/arabicnews.htm"
urlRequest = "http://www.google.com/cse/intl/ar/images/google_custom_search_watermark.gif"
class myWindow(QtWebKit.QWebView):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.page().mainFrame().setScrollBarPolicy(QtCore.Qt.Horizontal, QtCore.Qt.ScrollBarAlwaysOff)
self.page().mainFrame().setScrollBarPolicy(QtCore.Qt.Vertical, QtCore.Qt.ScrollBarAlwaysOff)
self.page().networkAccessManager().finished.connect(self.on_networkAccessManager_loadFinished)
self.load(QtCore.QUrl(urlTranslate))
@QtCore.pyqtSlot(QtNetwork.QNetworkReply)
def on_networkAccessManager_loadFinished(self, reply):
if urlRequest in reply.url().toString():
self.takeScreenshot()
def takeScreenshot(self):
fileName = self.title() + ".png"
self.page().setViewportSize(QtCore.QSize(1000, 2000))
image = QtGui.QImage(self.page().viewportSize(), QtGui.QImage.Format_ARGB32)
painter = QtGui.QPainter(image)
self.page().mainFrame().render(painter)
painter.end()
if not image.isNull():
image.save(fileName)
print "Succesfully saved '{0}'".format(fileName)
else:
print "Failed to save '{0}'".format(fileName)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())
i have fixed that full page load issue using below piece of code. 我使用以下代码修复了整个页面加载的问题。 now this code will stop after 30 seconds and it will take screenshot. 现在此代码将在30秒后停止,并需要截图。
self.web_page.networkAccessManager().finished.connect(self.on_networkAccessManager_loadFinished)
self._error = None
request = QNetworkRequest()
request.setUrl(QUrl(url))
self.timeout_timer = QTimer()
self.timeout_timer.timeout.connect(self._request_timed_out)
self.timeout_timer.start(30 * 1000) #30 seconds
self.web_page.mainFrame().load(request)
def _request_timed_out(self):
self._error = 'Custom request timeout value exceeded.'
print self._error
self.timeout_timer.stop()
self.web_page.killTimer(0)
self.web_page.loadFinished.emit(False)
self.takeScreenshot()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.