【问题标题】:Downloading images from Google Search using Python gives error?使用 Python 从 Google 搜索下载图像会出错?
【发布时间】:2013-12-21 20:36:10
【问题描述】:

这是我的代码:

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

# Define search term
searchTerm = "parrot"

# 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*10)+'&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
        my_url = myUrl['unescapedUrl']
        myopener.retrieve(myUrl['unescapedUrl'],str(count)+'.jpg')        

但是在下载了一些图片后,我得到了以下错误:

Traceback (most recent call last): File "C:\Python27\img_google3.py", line 37, in dataInfo = data['results'] TypeError: 'NoneType' object has no attribute 'getitem'

这可能是什么原因造成的?

我必须从 Google 下载图像,作为训练神经网络进行图像分类的一部分。

【问题讨论】:

  • 此外,我必须在系统中运行它才能下载至少 2000 张图像。所以,如果我在几次迭代后得到一个错误,这对我不利。我还有一些疑问,我会在课程中询问。请帮我 。谢谢。

标签: python web web-scraping


【解决方案1】:

错误消息告诉您results['responseData'] == None。您需要查看您在results(例如print(results))中实际获得的内容,以了解如何访问您想要的数据。

当您发生错误时,我得到以下信息:

{u'responseData': None, # hence the error
 u'responseDetails': u'out of range start', # what went wrong
 u'responseStatus': 400} # http response code for "Bad request"

最终您加载了一个网址(即https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=parrot&start=90&userip=MyIP),其中搜索结果根本不会那么高。我在results 中获得了一个合理的内容以获取较低的数字:...&start=0&...

您需要检查是否有任何返回,例如:

if results["responseStatus"] == 200:
    # response was OK, do your thing

此外,您可以简化构建 url 的代码并节省字符串连接:

template = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q={}&start={}&userip=MyIP'
url = template.format(searchTerm, str(i * 10))

【讨论】:

  • if results["responseStatus"] == 200: data = results['responseData'] dataInfo = data['results'] 我按照你说的更新了。它的工作。现在我可以下载 40 张图片。之后就停止了。我怎样才能增加我的下载量,我怎样才能进入谷歌图片搜索的下一页并继续下载,
  • 那么,在前 20 张图片之后,您从服务器得到什么响应?
  • 之前我遇到了错误。但现在似乎在大约 30 35 个独特的图像之后迭代下载相同的图像。