我正在开发一个小项目,使用 Python 3 从 Google Books API 检索有关图书的信息。为此,我调用 API,读出变量并将其存储在列表中。对于像“linkedin”这样的搜索,这非常有效。但是,当我输入“Google”时,它会从 JSON 输入中读取第二个标题。怎么会发生这种事?
请在下面找到我的代码(Google_Results 是我用来初始化变量的类):
import requests
def Book_Search(search_term):
parms = {"q": search_term, "maxResults": 3}
r = requests.get(url="https://www.googleapis.com/books/v1/volumes", params=parms)
print(r.url)
results = r.json()
i = 0
for result in results["items"]:
try:
isbn13 = str(result["volumeInfo"]["industryIdentifiers"][0]["identifier"])
isbn10 = str(result["volumeInfo"]["industryIdentifiers"][1]["identifier"])
title = str(result["volumeInfo"]["title"])
author = str(result["volumeInfo"]["authors"])[2:-2]
publisher = str(result["volumeInfo"]["publisher"])
published_date = str(result["volumeInfo"]["publishedDate"])
description = str(result["volumeInfo"]["description"])
pages = str(result["volumeInfo"]["pageCount"])
genre = str(result["volumeInfo"]["categories"])[2:-2]
language = str(result["volumeInfo"]["language"])
image_link = str(result["volumeInfo"]["imageLinks"]["thumbnail"])
dict = Google_Results(isbn13, isbn10, title, author, publisher, published_date, description, pages, genre,
language, image_link)
gr.append(dict)
print(gr[i].title)
i += 1
except:
pass
return
gr = []
Book_Search("Linkedin")
我是 Python 初学者,因此我们将不胜感激!
最佳答案
这样做是因为第一个条目的 volumeInfo
中没有 publisher
条目,因此它会引发 KeyError
和您的 except
捕获它。如果您要使用模糊数据,您必须考虑到它并不总是具有预期的结构。对于简单的情况,您可以依靠 dict.get()
及其 default
参数在缺少条目时返回“有效”默认条目。
此外,您的函数还存在一些概念问题 - 它依赖于全局 gr
,这是糟糕的设计,它隐藏了内置 dict
类型,并且它捕获所有异常,保证即使使用 SIGINT 也无法退出代码...我建议您将其转换为更理智的东西:
def book_search(search_term, max_results=3):
results = [] # a list to store the results
parms = {"q": search_term, "maxResults": max_results}
r = requests.get(url="https://www.googleapis.com/books/v1/volumes", params=parms)
try: # just in case the server doesn't return valid JSON
for result in r.json().get("items", []):
if "volumeInfo" not in result: # invalid entry - missing volumeInfo
continue
result_dict = {} # a dictionary to store our discovered fields
result = result["volumeInfo"] # all the data we're interested is in volumeInfo
isbns = result.get("industryIdentifiers", None) # capture ISBNs
if isinstance(isbns, list) and isbns:
for i, t in enumerate(("isbn10", "isbn13")):
if len(isbns) > i and isinstance(isbns[i], dict):
result_dict[t] = isbns[i].get("identifier", None)
result_dict["title"] = result.get("title", None)
authors = result.get("authors", None) # capture authors
if isinstance(authors, list) and len(authors) > 2: # you're slicing from 2
result_dict["author"] = str(authors[2:-2])
result_dict["publisher"] = result.get("publisher", None)
result_dict["published_date"] = result.get("publishedDate", None)
result_dict["description"] = result.get("description", None)
result_dict["pages"] = result.get("pageCount", None)
genres = result.get("authors", None) # capture genres
if isinstance(genres, list) and len(genres) > 2: # since you're slicing from 2
result_dict["genre"] = str(genres[2:-2])
result_dict["language"] = result.get("language", None)
result_dict["image_link"] = result.get("imageLinks", {}).get("thumbnail", None)
# make sure Google_Results accepts keyword arguments like title, author...
# and make them optional as they might not be in the returned result
gr = Google_Results(**result_dict)
results.append(gr) # add it to the results list
except ValueError:
return None # invalid response returned, you may raise an error instead
return results # return the results
然后您可以轻松检索某个术语的尽可能多的信息:
gr = book_search("Google")
而且,只要您的 Google_Results
类型使大部分条目成为可选,它对数据遗漏的容忍度就会高得多。
关于python - 从 JSON 获取错误结果 - Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45259067/