试图从google books获取数据,但显示错误‘'method’object is not subscriptable‘
我知道在类中使用方括号调用方法时会出现“TypeError:‘method’object is not subscriptable”错误。若要解决此错误,请确保仅在要调用的方法的名称后使用圆括号调用类的方法。
def books(request):
if request.method == "POST":
form = DashboardForm(request.POST)
text = request.POST['text']
url = 'https://www.googleapis.com/books/v1/volumes?q='+text
r = requests.get(url)
answer = r.json
result = []
for i in range(10):
result_dict= {
'title':answer['items'][i]['volumeInfo']['title'], # This line is showing error.
'subtitle': answer['items'][i]['volumeInfo']['title'].get('subtitle'),
'description': answer['items'][i]['volumeInfo']['title'].get('description'),
'count': answer['items'][i]['volumeInfo']['title'].get('pageCount'),
'categories': answer['items'][i]['volumeInfo']['title'].get('categories'),
'rating': answer['items'][i]['volumeInfo']['title'].get('pageRating'),
'thumbnail': answer['items'][i]['volumeInfo']['title'].get('imageLinks'),
'preview': answer['items'][i]['volumeInfo']['title'].get('previewLink')
}
result.append(result_dict)
context={
'form':form,
'results':result,
}
return render(request,'dashboard/books.html',context)
else:
form = DashboardForm()
context = {'form':form}
return render(request,'dashboard/books.html',context)
发布于 2021-07-19 17:07:41
在第7行,您可以这样写:
answer = r.json
但是r.json是一个你应该调用的方法:
answer = r.json()
https://stackoverflow.com/questions/68444291
复制相似问题