所以,我前段时间发现了这块魔法并且效果很好
import os
import sys
import time
from urllib import FancyURLopener
import urllib
import urllib2
import json as simplejson
searchTerm = "somethingsomething"
searchTerm = searchTerm.replace(' ','%20')
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()
count= 0
for i in range(0,10):
url = ('https://ajax.googleapis.com/ajax/services/search/images?' + 'v=1.0&q='+searchTerm+'&start='+str(i*4)+'&userip=MyIP')
print("url")
request = urllib2.Request(url, None, {'Referer': 'testing'})
response = urllib2.urlopen(request)
results = simplejson.load(response)
data = results['responseData']
dataInfo = data['results']
for myUrl in dataInfo:
count = count + 1
print("myUrl['unescapedUrl']")
myopener.retrieve(myUrl['unescapedUrl'],str(count)+'.jpg')
time.sleep(1)
几天前我想再次使用它并收到此错误:
dataInfo = data ['结果'] TypeError:' NoneType'对象是不可取消的
我使用的是与之前版本相同的python(2.6) 我不知道如何解决这个问题,是api还是什么,有人吗?
(澄清一下:我正在将这个用于我的艺术项目,我不知道如何"真的"代码,所以请大家,对我温柔;)我知道有我的类似问题,但我不知道如何应用我的问题答案......)
答案 0 :(得分:0)
您的代码未正确缩进。此外,results['responseData']
为None
,responseDetails
表示This API is no longer available.
因此,您根本没有results['responseData']['results']
。
>>> url = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=somethingsomething&start=36&userip=MyIP'
>>>
>>> request = urllib2.Request(url, None, {'Referer': 'testing'})
>>> response = urllib2.urlopen(request)
>>> results = simplejson.load(response)
>>> print(results)
{u'responseData': None, u'responseDetails': u'This API is no longer available.', u'responseStatus': 403}
>>>
>>>
>>> data = results['responseData']
>>> dataInfo = data['results']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'
>>>