使用Google自定义搜索API下载图片

6 投票
2 回答
18770 浏览
提问于 2025-04-18 01:22

我之前在Python中使用了谷歌图片API,下载了前20张图片,代码如下:

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)

但现在我想用谷歌自定义搜索来实现这个功能,这样可以得到更好的结果。我知道我需要注册一个API密钥,但我找不到像我之前那样简单的示例代码。有没有人能帮我一下,我在谷歌的文档里真的很迷茫。

看起来免费的API有一些限制,每天只能请求100次,是这样吗?

编辑:我现在在这里,但还是不行

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]) 

2 个回答

0

我找到了一些可以下载图片的API,这些可以帮助你创建图片数据集,可能你会觉得这些很有用!

  1. 第一个链接

  2. 第二个链接

从文档来看,我觉得第二个链接特别好!

21

你可以使用这个 Google API Python 客户端库 来进行开发。

示例:

这里

from apiclient.discovery import build

service = build("customsearch", "v1",
               developerKey="** your developer key **")

res = service.cse().list(
    q='butterfly',
    cx=' ** your cx **',
    searchType='image',
    num=3,
    imgType='clipart',
    fileType='png',
    safe= 'off'
).execute()

if not 'items' in res:
    print 'No result !!\nres is: {}'.format(res)
else:
    for item in res['items']:
        print('{}:\n\t{}'.format(item['title'], item['link']))

输出:

Clipart - Butterfly:
        http://openclipart.org/image/800px/svg_to_png/3965/jonata_Butterfly.png
Animal, Butterfly, Insect, Nature - Free image - 158831:
        http://pixabay.com/static/uploads/photo/2013/07/13/11/51/animal-158831_640.png
Clipart - Monarch Butterfly:
        http://openclipart.org/image/800px/svg_to_png/110023/Monarch_Butterfly_by_Merlin2525.png

是的,免费版是有一些限制的,你可以通过 Google 开发者控制台 来监控这些限制:

这里

注意:

去你的 自定义搜索引擎,然后选择你的 自定义搜索引擎,接着在 基本设置 标签中,设置 图片搜索 选项为 开启,在 搜索的网站 部分,选择 搜索整个网络,但强调包含的网站 选项。

链接:

撰写回答