Taking a screenshot of a website
A common scraping task is to create a screenshot of a website. In Python we can create a thumbnail using selenium and webdriver.
Getting ready
The script for this recipe is 04/08_create_website_screenshot.py
. Also, make sure you have selenium in your path and have installed the Python library.
How to do it
Run the script for the recipe. The code in the script is the following:
from core.website_screenshot_generator import WebsiteScreenshotGenerator from core.file_blob_writer import FileBlobWriter from os.path import expanduser # get the screenshot image_bytes = WebsiteScreenshotGenerator().capture("http://espn.go.com", 500, 500).image_bytes # save it to a file FileBlobWriter(expanduser("~")).write("website_screenshot.png", image_bytes)
A WebsiteScreenshotGenerator
object is created, and then its capture method is called, passing the URL of the website to capture, and a desired width in pixels for the image.
This creates a Pillow image that can be accessed...