Downloading and saving images to the local file system
Sometimes when scraping we just download and parse data, such as HTML, to extract some data, and then throw out what we read. Other times, we want to keep the downloaded content by storing it as a file.
How to do it
The code example for this recipe is in the 04/05_save_image_as_file.py
file. The portion of the file of importance is:
# download the image item = URLUtility(const.ApodEclipseImage()) # create a file writer to write the data FileBlobWriter(expanduser("~")).write(item.filename, item.data)
Run the script with your Python interpreter and you will get the following output:
Reading URL: https://apod.nasa.gov/apod/image/1709/BT5643s.jpg Read 171014 bytes Attempting to write 171014 bytes to BT5643s.jpg: The write was successful
How it works
The sample simply writes the data to a file using standard Python file access functions. It does it in an object oriented manner by using a standard interface for writing data and with a file based...