gpt4 book ai didi

python - 如何使用 Python 截取网站的屏幕截图/图像?

转载 作者:IT老高 更新时间:2023-10-28 20:32:45 32 4
gpt4 key购买 nike

我想要实现的是从 python 中的任何网站获取网站截图。

环境:Linux

最佳答案

这是一个使用 webkit 的简单解决方案: http://webscraping.com/blog/Webpage-screenshots-with-webkit/

import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

class Screenshot(QWebView):
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
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
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

s = Screenshot()
s.capture('http://webscraping.com', 'website.png')
s.capture('http://webscraping.com/blog', 'blog.png')

关于python - 如何使用 Python 截取网站的屏幕截图/图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1197172/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com