【发布时间】:2019-06-26 02:31:18
【问题描述】:
所以,我正在尝试使用 Goodreads 的 API 在 Python 中编写 Goodreads Information Fetcher 应用程序。我目前正在开发应用程序的第一个函数,该函数将从 API 获取信息,API 返回一个 XML 文件。
我解析了 XML 文件并将其转换为 JSON 文件,然后进一步将其转换为字典。但我似乎仍然无法从中提取信息,我在这里查找了其他帖子,但没有任何效果。
main.py
def get_author_books(authorId):
url = "https://www.goodreads.com/author/list/{}?format=xml&key={}".format(authorId, key)
r = requests.get(url)
xml_file = r.content
json_file = json.dumps(xmltodict.parse(xml_file))
data = json.loads(json_file)
print("Book Name: " + str(data[0]["GoodreadsResponse"]["author"]["books"]["book"]))
我希望输出给我字典中第一本书的名称。
Here 是 Goodreads 提供的示例 XML 文件。
【问题讨论】:
标签: python json api dictionary python-requests