Saturday, April 6, 2013

Web Image Grabbing Robot Using Google Image API

This time, I am facing a problem to grab corresponding images from a keyword list. Most of the time, we do Google Image Search, and find out the image we want. However, this will be inefficient when it comes to few hundreds of keywords.

Therefore, the program I wrote here automatic read keywords from the list, and call the Google Image API to find out the first three images relating to this keyword. The images are showed in URL. The reason for showing the first three images is that not all the time Google give out the one we want in the first element of the results. I've also written a website in PHP which allow people to select the image from these three images, and automatically grab the one which is selected to the server. As a result, you only have to check through which image you want to store in the end, you don't have to do any search or any downloading.

Python Program:
import urllib2
import simplejson

keyword = raw_input("Image Search >> ")
keyword_encoded = urllib2.quote(keyword, '')

url = ('' +
       'v=1.0&q=' + keyword_encoded + '&userip=IP-INSERT-HERE-HERE')

request  = urllib2.Request(url, None, {'Referer': ''})
response = urllib2.urlopen(request)

results = simplejson.load(response)

for i in range(3):
 print results['responseData']['results'][i]['url']