aviraldg
aviraldg

Reputation: 9154

Does Google have any official Image API for Python?

I want to search for images using a Python script and pull them off the web. Is there an official API for this? What would be the best way to do this in case there's no API for it?

Upvotes: 2

Views: 1449

Answers (2)

Jaime Ivan Cervantes
Jaime Ivan Cervantes

Reputation: 3697

I use the following code in Python to search for Google images and download the images to my computer and it uses JSON and FancyURLopener to retrieve the data:

import os
import sys
import time
from urllib import FancyURLopener
import urllib2
import simplejson

# Define search term
searchTerm = "hello world"

# Replace spaces ' ' in search term for '%20' in order to comply with request
searchTerm = searchTerm.replace(' ','%20')


# Start FancyURLopener with defined version 
class MyOpener(FancyURLopener): 
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'
myopener = MyOpener()

# Set count to 0
count= 0

for i in range(0,10):
    # Notice that the start changes for each iteration in order to request a new set of images for each loop
    url = ('https://ajax.googleapis.com/ajax/services/search/images?' + 'v=1.0&q='+searchTerm+'&start='+str(i*4)+'&userip=MyIP')
    print url
    request = urllib2.Request(url, None, {'Referer': 'testing'})
    response = urllib2.urlopen(request)

    # Get results using JSON
    results = simplejson.load(response)
    data = results['responseData']
    dataInfo = data['results']

    # Iterate for each result and get unescaped url
    for myUrl in dataInfo:
        count = count + 1
        print myUrl['unescapedUrl']

        myopener.retrieve(myUrl['unescapedUrl'],str(count)+'.jpg')

    # Sleep for one second to prevent IP blocking from Google
    time.sleep(1)

You can also find very useful information here.

Upvotes: 0

leoluk
leoluk

Reputation: 12951

Google has an official API for accessing search features, including images. They use JSON for communication, so it's easily accessible via Python. They are many Python wrappers around it like this one.

Upvotes: 5

Related Questions