在 PyQt5 中截取网页的屏幕截图 [英] Taking a screenshot of a web page in PyQt5
本文介绍了在 PyQt5 中截取网页的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 PyQt5 截取网页的截图.(一个完整的网页,包括用户除非向下滚动才能看到的内容.)
I would like to use PyQt5 to take a screenshot of a webpage. (A full webpage, including the stuff a user wouldn't see unless they scrolled down.)
据说,可以使用 QtWebEngine 在 PyQt5 中做到这一点.你会怎么做呢?我特别不希望用户看到浏览器窗口打开或呈现.我只想要一个 PNG 文件的屏幕截图.
Supposedly, it is possible to do this in PyQt5 using QtWebEngine. How would you do it though? I specifically don't want the user to see a browser window opening or rendering. I just want a screenshot in a PNG file.
推荐答案
以下是 QtWebEngine(版本 5.12)的示例:
Here is an example for QtWebEngine (version 5.12):
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, QUrl, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
class Screenshot(QWebEngineView):
def capture(self, url, output_file):
self.output_file = output_file
self.load(QUrl(url))
self.loadFinished.connect(self.on_loaded)
# Create hidden view without scrollbars
self.setAttribute(Qt.WA_DontShowOnScreen)
self.page().settings().setAttribute(
QWebEngineSettings.ShowScrollBars, False)
self.show()
def on_loaded(self):
size = self.page().contentsSize().toSize()
self.resize(size)
# Wait for resize
QTimer.singleShot(1000, self.take_screenshot)
def take_screenshot(self):
self.grab().save(self.output_file, b'PNG')
self.app.quit()
app = QApplication(sys.argv)
s = Screenshot()
s.app = app
s.capture('https://pypi.org/project/PyQt5/', 'webpage.png')
sys.exit(app.exec_())
这篇关于在 PyQt5 中截取网页的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文