TypeError:'NoneType'对象没有用于谷歌搜索的属性'__getitem__'

时间:2015-12-07 01:34:41

标签: image search nonetype

我一直在使用我在这里找到的代码(信用无空) https://www.reddit.com/r/osx/comments/3dxgv9/automator_script_or_any_other_way_to_automate_a/

  import json
  import os
  import time
  import requests
  from PIL import Image
  from StringIO import StringIO
  from requests.exceptions import ConnectionError

  def go(query, path,images):
    """Download full size images from Google image search.

    Don't print or republish images without permission.
    I used this to train a learning algorithm.
    """
    BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
               'v=1.0&q=' + query + '&start=%d'

    BASE_PATH = os.path.join(path, query)

    if not os.path.exists(BASE_PATH):
      os.makedirs(BASE_PATH)

    start = 0 # Google's start query string parameter for pagination.
    counter = 0
    while start < images: # Google will only return a max of 56 results.
      r = requests.get(BASE_URL % start)
      for image_info in json.loads(r.text)['responseData']['results']:
        counter += 1
        if images + 1 == counter:
            break
        url = image_info['unescapedUrl']
        try:
          image_r = requests.get(url)
        except ConnectionError, e:
          print 'could not download %s' % url
          continue

        # Remove file-system path characters from name.
        title = query + ' ' + str(counter)

        file = open(os.path.join(BASE_PATH, '%s.jpg') % title, 'w')
        try:
          Image.open(StringIO(image_r.content)).save(file, 'JPEG')
        except IOError, e:
          # Throw away some gifs...blegh.
          print 'could not save %s' % url
          images += 1
          continue
        finally:
          file.close()

  #print start
  start += 4 # 4 images per page.

  # Be nice to Google and they'll be nice back :)
  time.sleep(1.5)


  print "This script can be stopped at any time by pressing ctrl + c"
  print " "
  print "Enter the Database root path between quotation marks eg:"
  print '"/Users/Anon/Desktop/Test"'
  Root_Path = input("Path = ")
  print " "
  print "How many queries would you like to do? eg:"
  print "6"
  Queries = input("Amount = ")
  print " "
  print "How many pictures would you like to download of each query? eg:"
  print "3"
  Pictures = input("Amount = ")
  print ""

  print "Now follows entering the queries. A folder with the name of the query will"
  print "appear with in that folder " + str(Pictures) + "pictures of the specified query name"
  print " "
  print "Name of the Query between quotation marks eg:"
  print '"Apple"'
  print " "
  for z in range(Queries):
      Name = input("Name = ")
      go(str(Name), Root_Path, Pictures)
      print " "

但是我不断收到以下错误回复:

Traceback (most recent call last):
  File "C:\Python27\reddit.py", line 82, in <module>
    go(str(Name), Root_Path, Pictures)
  File "C:\Python27\reddit.py", line 27, in go
    for image_info in json.loads(r.text)['responseData']['results']:
TypeError: 'NoneType' object has no attribute '__getitem__'

我读了这个帖子Python TypeError: 'NoneType' object has no attribute '__getitem__' for Google Search并尝试应用修复程序,但我似乎无法找到解决方案。

我正在尝试从谷歌下载一些图片。感谢

0 个答案:

没有答案