Python script for "Google Image Search"

I checked the Google search APIs and it seems that they have not released an API to search for "Images". So, I was wondering if there is a python script / library through which I can automate the "image search" function.

+8
python automation google-image-search
source share
3 answers

There are no APIs available, but you can analyze the page and simulate a browser, but I don’t know how much data you need to analyze, because Google can restrict or block access.

You can mimic the browser simply by using urllib and setting the correct headers, but if you think that parsing complex web pages might be difficult using python, you can directly use a mute browser like phontomjs , it is trivial to get the correct elements inside the browser using javascript / DOM

Note before you try all this check google TOS

+2
source share

It was annoying to realize that I thought I would throw a comment on the first stackoverflow result related to python for a "Google image search script". The most annoying part of all this is setting up the correct application and user search engine (CSE) in the Google web interface, but as soon as you have the api and CSE key, define them in your environment and do something like:

#!/usr/bin/env python # save top 10 google image search results to current directory # https://developers.google.com/custom-search/json-api/v1/using_rest import requests import os import sys import re import shutil url = 'https://www.googleapis.com/customsearch/v1?key={}&cx={}&searchType=image&q={}' apiKey = os.environ['GOOGLE_IMAGE_APIKEY'] cx = os.environ['GOOGLE_CSE_ID'] q = sys.argv[1] i = 1 for result in requests.get(url.format(apiKey, cx, q)).json()['items']: link = result['link'] image = requests.get(link, stream=True) if image.status_code == 200: m = re.search(r'[^\.]+$', link) filename = './{}-{}.{}'.format(q, i, m.group()) with open(filename, 'wb') as f: image.raw.decode_content = True shutil.copyfileobj(image.raw, f) i += 1 
+2
source share

You can try the following: https://developers.google.com/image-search/v1/jsondevguide#json_snippets_python It is deprecated, but it seems to work.

-one
source share

All Articles