Python Code

Google Image Search

And How To Use Python to automate it!

Step 1 – install Google-Images-Search
Step 2 – follow the installation instructions
env variables
Step 3 – set up the .env file with the API key and CX code you got when you generated your API credentials with Google.
Note : Make sure you enabled image search by setting it to “on”


from google_images_search import GoogleImagesSearch
from dotenv import load_dotenv
import os
# load the API key and CX code from .env file

# create an 'ims' sub directory if it doesn't already exists
if not os.path.exists('ims/'):
pathz = 'ims/'
# Get env variables
DK = os.environ.get('DEVELOPER_KEY')
CX = os.environ.get('CX')

# custom progressbar function
def my_progressbar(url, progress):
    print(url + " " + progress + "%")

# create google images search - object
gis = GoogleImagesSearch(DK, CX, progressbar_fn=my_progressbar)
# using contextual mode (Curses)
with GoogleImagesSearch(DK, CX) as gis:
    # define search params:
    _search_params = {"q": "tree", 
        "num": 3, 
        "safe": "off", 
        "fileType": "png"
        }, path_to_dir=pathz)

3 images of trees – here is one of them!

Limit Exceeded!

If you see “Error 429 when requesting…” or…


then you may have exceeded your daily allowance for API calls. So be careful when testing the code not to make too many requests.

Quota reached!

To install our code you can use pip:

pip install gimdl