Python script for "Google search by image"
Asked Answered
H

3

8

I have checked Google Search API's and it seems that they have not released any API for searching "Images". So, I was wondering if there exists a python script/library through which I can automate the "search by image feature".

Husbandry answered 2/12, 2011 at 20:42 Comment(2)
I'm guessing Google is worried about the resources that would incur. It would be a great thing thoForlorn
This python script might help: bit.ly/QjIy21Tenotomy
T
3

This was annoying enough to figure out that I thought I'd throw a comment on the first python-related stackoverflow result for "script google image search". The most annoying part of all this is setting up your proper application and custom search engine (CSE) in Google's web UI, but once you have your api key and CSE, 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
Translucent answered 24/2, 2016 at 20:22 Comment(0)
H
3

There is no API available but you are can parse the page and imitate the browser, but I don't know how much data you need to parse because google may limit or block access.

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

Note before trying all this check google's TOS

Heirloom answered 2/12, 2011 at 21:15 Comment(2)
For "google search by image", the image has to be dragged and dropped on to the screen. I am not sure, how can I automate that using pythonHusbandry
when you drag drop image, browser send the image file data to google, that you can do from python also, but it may be diffcult to automate all parts so that is why best bet is to go phantomjs routeHeirloom
T
3

This was annoying enough to figure out that I thought I'd throw a comment on the first python-related stackoverflow result for "script google image search". The most annoying part of all this is setting up your proper application and custom search engine (CSE) in Google's web UI, but once you have your api key and CSE, 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
Translucent answered 24/2, 2016 at 20:22 Comment(0)
H
-1

You can try this: https://developers.google.com/image-search/v1/jsondevguide#json_snippets_python It's deprecated, but seems to work.

Hackler answered 31/10, 2012 at 15:32 Comment(1)
Unfortunately this does not provide the "search by image" functionality, it only supports using search terms in text. Full docs on the JSON API can be found at developers.google.com/image-search/v1/devguideEphrem

© 2022 - 2024 — McMap. All rights reserved.