Downloading images from Google Search using Python gives error?

后端 未结 1 423

Here is my code:

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

# Define search term
searchTerm = \"parrot\         


        
相关标签:
1条回答
  • 2021-01-26 06:41

    The error message tells you that results['responseData'] == None. You need to look at what you actually get in results (e.g. print(results)) to figure out how to access the data you want.

    I get the following when your error occurs:

    {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"
    

    Eventually you load a url (i.e. https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=parrot&start=90&userip=MyIP) where the search results simply don't go that high. I get a sensible content in results for lower numbers: ...&start=0&....

    You need to check whether you get anything back, e.g.:

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

    Also, you could make your url-building code simpler and save on the string concatenation:

    template = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q={}&start={}&userip=MyIP'
    url = template.format(searchTerm, str(i * 10))
    
    0 讨论(0)
提交回复
热议问题