What I want to achieve is to get a website screenshot from any website in python.
Env: Linux
Ensure that your browser is that “active window” by clicking anywhere in the browser window. Press Alt + Print Screen (may also say Prnt Scrn , or another variation). This takes a screenshot of the browser and copies it to the clipboard. Paste the image into a ticket or email by pressing Ctrl + V .
For capturing the screenshot, save_screenshot() method is available. This method takes the full page screenshot. There is no in built method to capture an element. To achieve this we have to crop the image of the full page to the particular size of the element.
Take the first screenshot using Selenium WebDriver and function save_screenshot() Take a screenshot using Python selenium-screenshot – Using Screenshot_Clipping from the Screenshot module, and the full_Screenshot() function.
Here is a simple solution using 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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With