本文介绍了用google自定义搜索API下载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经在python中使用过google image api,用下面的代码下载第一张图片的结果: import os import sys 导入时间$ b $ from urllib import FancyURLopener 导入urllib2 导入simplejson $ b searchTerm =Cat #为了符合请求,在'%20'的搜索字词中替换空格'' searchTerm = searchTerm.replace('','%20') #开始使用已定义版本的FancyURLopener 类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() #将计数设置为0 计数= 0 对于范围(0,4)中的i:#请注意,每次迭代开始更改以便为每个循环请求一组新的图像 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) #使用JSON获取结果 results = simplejson.load(response) data =结果['responseData'] dataInfo = data ['results'] #迭代每个结果并获取未转义的url for myUrl in dataInfo: count = count + 1 print myUrl ['unescapedUrl'] os.chdir(newpath) myopener.retrieve(myUrl ['unescapedUrl'],str(num)+' - '+ str计数)) #睡眠一秒钟,以防止谷歌IP阻止 time.sleep(3) 但现在我想使用谷歌自定义搜索来做到这一点,以获得更好的结果。我明白,我应该注册获得一个APIKey,但我没有找到任何简单的例子作为我发布的代码。有人可以帮忙,我真的迷失在谷歌文档中。 显然有免费api的限制,每天100个请求,是否正确? p> 编辑:我在这里rightnow,但仍然无法工作 import os 进口系统进口时间$ b $来自urllib进口FancyURLopener 进口urllib2 进口simplejson 进口cStringIO 进口pprint searchTerm =Cat #开始使用已定义版本的FancyURLopener 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键= API_KEY&安培; CX = 017576662512468239146:omuauf_lfve '+' &安培; q = '+ SEARCHTERM +' &安培;检索类别=图像 '+' &安培;开始= 0 '+' &安培; imgSize = XLARGE | xxlarge | huge' print url request = urllib2.Request(url,None,{'Referer :'testing'}) response = urllib2.urlopen(request) #使用JSON获取结果 data = json.load(response) pprint.PrettyPrinter(indent = 4).pprint(data ['items'] [0]) 解决方案您可以使用此,然后选择您的自定义搜索引擎,然后在 Basics 标签, set 图片搜索选项添加到 ON ,并为要搜索的网站部分,选择搜索整个但强调包含网站选项。 https://google-api-client-libraries.appspot.com/documentation/customsearch/v1/python/latest/customsearch_v1.cse.html https://developers.google.com/custom-search/json-api/v1/reference/cse/list https://www.google.com/cse/all https: //developers.google.com/api-client-library/python/apis/customsearch/v1 https://console.developers.google.com/project https://developers.google.com/api-client-library/python/start/get_started https://developers.google.com / api-client-library / python / guide / aaa_apikeys I have used google image api in python to download 20 first image result with the following code:import osimport sysimport timefrom urllib import FancyURLopenerimport urllib2import simplejsonsearchTerm = "Cat"# Replace spaces ' ' in search term for '%20' in order to comply with requestsearchTerm = searchTerm.replace(' ','%20')# Start FancyURLopener with defined versionclass 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 0count=0for 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 workimport osimport sysimport timefrom urllib import FancyURLopenerimport urllib2import simplejsonimport cStringIOimport pprintsearchTerm="Cat"# Start FancyURLopener with defined versionclass 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 urlrequest = urllib2.Request(url, None, {'Referer': 'testing'})response = urllib2.urlopen(request) # Get results using JSONdata = json.load(response)pprint.PrettyPrinter(indent=4).pprint(data['items'][0]) 解决方案 You can use this Google APIs Client Library for Python.Demo:Here is a sample (i change it to):from apiclient.discovery import buildservice = 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']))Output:Clipart - Butterfly: http://openclipart.org/image/800px/svg_to_png/3965/jonata_Butterfly.pngAnimal, Butterfly, Insect, Nature - Free image - 158831: http://pixabay.com/static/uploads/photo/2013/07/13/11/51/animal-158831_640.pngClipart - Monarch Butterfly: http://openclipart.org/image/800px/svg_to_png/110023/Monarch_Butterfly_by_Merlin2525.pngYes, there is a limitation for Free edition and you can monitor it from Google developer console:Note:Go to your Custom Search Engine, then select your custom search engine, then in Basics tab,set Image search option to ON, and for Sites to search section, select Search the entire web but emphasize included site option.Links:https://google-api-client-libraries.appspot.com/documentation/customsearch/v1/python/latest/customsearch_v1.cse.htmlhttps://developers.google.com/custom-search/json-api/v1/reference/cse/listhttps://www.google.com/cse/allhttps://developers.google.com/api-client-library/python/apis/customsearch/v1https://console.developers.google.com/projecthttps://developers.google.com/api-client-library/python/start/get_startedhttps://developers.google.com/api-client-library/python/guide/aaa_apikeys 这篇关于用google自定义搜索API下载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 10:51