如何使用 python 从 google 本书 api 中获取数据
How to fetch data from google book api using python
我正在研究书籍推荐系统,所以使用 ml 我得到了存储在列表 book_list 中的推荐,所以我尝试使用 google 书籍 api 获取book_list
中所列项目的书籍封面
but show KeyError: 'imageLinks'
按键错误:'imageLinks'
这里是app.py,在这里用户会在表单中输入书名,得到推荐后会得到推荐我已经尝试获取图片链接
@app.route('/search',methods=['GET','POST'])
def search():
choice = request.args.get('search')
# removing all the characters except alphabets and numbers.
# passing the choice to the recommend() function
# passing the choice to the recommend() function
books = recommend(choice)
image=get_image(books)
# if rocommendation is a string and not list then it is else part of the
# recommend() function.
if type(books) == type('string'):
return render_template('read.html', book=books,image=image)
else:
return render_template('read.html', book=books,image=image)
这是我尝试获取图像链接的代码
def get_image(books):
img=[]
for i in range(len(books)):
response=requests.get(url + books[i])
obj=json.loads(response.text)
w=obj['items'][0]['volumeInfo']['imageLinks']['thumbnail']
img.append(w)
return img
这是url,我在运行 flask app
之前放了
url='https://www.googleapis.com/books/v1/volumes?q='
app=Flask(__name__)
volumeInfo 中的图像链接并非适用于每本书
例如
https://www.googleapis.com/books/v1/volumes?q=test
“0”一书“Software-Test für Embedded Systems”不包含图像链接
你需要try/catch那个案例或者事先查看它是否存在
for...
...
try:
w=obj['items'][0]['volumeInfo']['imageLinks']['thumbnail']
img.append(w)
except:
print("imgageLinks not found")
return img
...
我正在研究书籍推荐系统,所以使用 ml 我得到了存储在列表 book_list 中的推荐,所以我尝试使用 google 书籍 api 获取book_list
中所列项目的书籍封面but show KeyError: 'imageLinks'
按键错误:'imageLinks'
这里是app.py,在这里用户会在表单中输入书名,得到推荐后会得到推荐我已经尝试获取图片链接
@app.route('/search',methods=['GET','POST'])
def search():
choice = request.args.get('search')
# removing all the characters except alphabets and numbers.
# passing the choice to the recommend() function
# passing the choice to the recommend() function
books = recommend(choice)
image=get_image(books)
# if rocommendation is a string and not list then it is else part of the
# recommend() function.
if type(books) == type('string'):
return render_template('read.html', book=books,image=image)
else:
return render_template('read.html', book=books,image=image)
这是我尝试获取图像链接的代码
def get_image(books):
img=[]
for i in range(len(books)):
response=requests.get(url + books[i])
obj=json.loads(response.text)
w=obj['items'][0]['volumeInfo']['imageLinks']['thumbnail']
img.append(w)
return img
这是url,我在运行 flask app
之前放了url='https://www.googleapis.com/books/v1/volumes?q='
app=Flask(__name__)
volumeInfo 中的图像链接并非适用于每本书
例如
https://www.googleapis.com/books/v1/volumes?q=test
“0”一书“Software-Test für Embedded Systems”不包含图像链接
你需要try/catch那个案例或者事先查看它是否存在
for...
...
try:
w=obj['items'][0]['volumeInfo']['imageLinks']['thumbnail']
img.append(w)
except:
print("imgageLinks not found")
return img
...