在 Python 中我收到一个错误:
Exception: (<type 'exceptions.AttributeError'>,
AttributeError("'str' object has no attribute 'read'",), <traceback object at 0x1543ab8>)
给定 python 代码:
def getEntries (self, sub):
url = 'http://www.reddit.com/'
if (sub != ''):
url += 'r/' + sub
request = urllib2.Request (url +
'.json', None, {'User-Agent' : 'Reddit desktop client by /user/RobinJ1995/'})
response = urllib2.urlopen (request)
jsonStr = response.read()
return json.load(jsonStr)['data']['children']
这个错误是什么意思?我做了什么导致了这个错误?
问题是对于json.load
你应该传递一个类似文件的对象read
函数定义。所以你要么使用json.load(response) http://docs.python.org/library/json.html#json.load or json.loads(response.read()) http://docs.python.org/library/json.html#json.loads.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)