Google Image Search
And How To Use Python to automate it!
code:
from google_images_search import GoogleImagesSearch
from dotenv import load_dotenv
import os
# load the API key and CX code from .env file
load_dotenv()
# create an 'ims' sub directory if it doesn't already exists
if not os.path.exists('ims/'):
os.mkdir('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"
}
gis.search(search_params=_search_params, path_to_dir=pathz)
print("\nDone\n")
Limit Exceeded!
If you see “Error 429 when requesting…” or…
RESOURCE_EXHAUSTED
then you may have exceeded your daily allowance for API calls. So be careful when testing the code not to make too many requests.
To install our code you can use pip:
pip install gimdl