我将我的应用程序部署到 Heroku,它向我显示了一个关于“items”的“keyError”。我的代码在本地运行得很好,但不能在 Heroku 上运行。有人知道我该如何解决吗? 非常感谢!
我的代码:
def getbooktitle():
if request.method == "POST":
file = request.files['image']
img = Image.open(file.stream)
booktitle = pytesseract.image_to_string(img)
api_key = "myapikey"
url = f"https://www.googleapis.com/books/v1/volumes?q={booktitle}&key={api_key}"
response = requests.get(url)
json_response = response.json()
volume_info = json_response['items'][0]['volumeInfo']
title = volume_info.get('title')
author = volume_info.get('authors')
imageslink = volume_info['imageLinks']
smallimage = imageslink['smallThumbnail']
return jsonify({"books": [{'title': title, 'author': author[0], 'imageLink': smallimage}]})
查看日志:
2021-07-20T15:50:16.826905+00:00 app[web.1]: [2021-07-20 15:50:16,823] ERROR in app: Exception on /getbook [POST]
2021-07-20T15:50:16.826914+00:00 app[web.1]: Traceback (most recent call last):
2021-07-20T15:50:16.826915+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
2021-07-20T15:50:16.826915+00:00 app[web.1]: response = self.full_dispatch_request()
2021-07-20T15:50:16.826917+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
2021-07-20T15:50:16.826917+00:00 app[web.1]: rv = self.handle_user_exception(e)
2021-07-20T15:50:16.826918+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
2021-07-20T15:50:16.826918+00:00 app[web.1]: rv = self.dispatch_request()
2021-07-20T15:50:16.826919+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
2021-07-20T15:50:16.826919+00:00 app[web.1]: return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2021-07-20T15:50:16.826920+00:00 app[web.1]: File "/app/app.py", line 21, in getbooktitle
2021-07-20T15:50:16.826921+00:00 app[web.1]: volume_info = json_response["items"][0]["volumeInfo"]
2021-07-20T15:50:16.826922+00:00 app[web.1]: KeyError: 'items'