[英]'method' object is not subscriptable trying for google api books
Trying to get data from google books API but it is showing the error ' 'method' object is not subscriptable'试图从谷歌图书 API 获取数据,但显示错误“'方法'对象不可下标”
I know the “TypeError: 'method' object is not subscriptable” error is raised when you use square brackets to call a method inside a class.我知道当您使用方括号调用类中的方法时会引发“TypeError: 'method' object is not subscriptable”错误。 To solve this error, make sure that you only call methods of a class using round brackets after the name of the method you want to call.
要解决此错误,请确保仅在要调用的方法名称后使用圆括号调用类的方法。
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)
In line 7, you write:在第 7 行,你写:
answer = r.json
But r.json is a method which you should call:但是 r.json 是一种您应该调用的方法:
answer = r.json()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.