Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

python - download images with google custom search api

I have used google image api in python to download 20 first image result with the following code:

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



searchTerm = "Cat"

# 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,4):
    # 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='+searchTerm7+'&start='+str(i*4)+'&userip=MyIP&imgsz=xlarge|xxlarge|huge')
  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']
    os.chdir(newpath)
    myopener.retrieve(myUrl['unescapedUrl'],str(num)+'-'+str(count))

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

But now i would like to use google custom search to do that, in order to get better result. I have understand that i should register to get a APIKey but i did'nt find any simple example as the code i post. Does some one can help, i am really lost in the google documentation.

Visibly there is restriction for the free api, 100 request a day, is that correct?

Edit: I am here rightnow, but still not work

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


searchTerm="Cat"

# 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()


url='https://www.googleapis.com/customsearch/v1?key=API_KEY&cx=017576662512468239146:omuauf_lfve'+'&q='+searchTerm+'&searchType=image'+'&start=0'+'&imgSize=xlarge|xxlarge|huge'
print url
request = urllib2.Request(url, None, {'Referer': 'testing'})
response = urllib2.urlopen(request)

    # Get results using JSON

data = json.load(response)
pprint.PrettyPrinter(indent=4).pprint(data['items'][0]) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...