Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I take a screenshot/image of a website using Python?

What I want to achieve is to get a website screenshot from any website in python.

Env: Linux

like image 549
Esteban Feldman Avatar asked Jul 28 '09 22:07

Esteban Feldman


People also ask

How do I take a screenshot of an image of a Web page?

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 .

How do you take a screenshot of a specific element in Python?

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.

How do I take a screenshot using selenium Python?

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.


1 Answers

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') 
like image 72
hoju Avatar answered Sep 20 '22 05:09

hoju