How to take screenshots in python?

7.4k Views Asked by At

I've an idea and want to implement it. But I'm not sure if it's gonna work. So, wanted to get your inputs.

 I would like to take screenshots of a url. 

Say, when I open a web-site www.espncricinfo.com , I would like to take screenshot of that page and save locally. This saved image can be converted to GIF later on. Can this be achieved through python ? Any suggestions/inputs to make it ?

Updated

And also is it possible to capture screenshot in headless-browser ?

Any possibilities to launch the browser in headless mode (non-GUI) and then take the screenshot of particular area of web-page ?

3

There are 3 best solutions below

4
On BEST ANSWER

To take a screenshot using python:

import pyscreenshot as ImageGrab

im = ImageGrab.grab()

im.save('path/to/image/folder/image_name.png')

im.show()
0
On

Yes and no, if you send a request with urllib you will get the HTML in return, which is step one to displaying a webpage. But you have to build that webpage from that with a browser engine, otherwise all you will see is a bunch of text.

There are some python libraries that can do this, such as pywebkitgtk, but those are probably not going to give you the best experience and support.

Another thing you could try is to use crod and firefox/chrome/whatever and then use python to automate the process.

Oh, and by the way, I strongly recommend upgrading to python3

0
On

I know it is a bit to late now, but just saw this question and in case anyone else is looking for a mighty tool, to do screenshots and also use headless browsers with python... I just want to recommend to look more deeply into https://www.seleniumhq.org/. There are many tutorials on youtube available. However, this is IMHO the most accurate framework to execute your task, IMHO. To make screenshots, you can then define the screen resolution etc.