【问题标题】:How to get the authors name from Google books api?如何从 Google 图书 api 获取作者姓名?
【发布时间】:2021-12-17 14:53:33
【问题描述】:

当我使用此链接https://www.googleapis.com/books/v1/volumes?q=9780310709626 搜索这本书时,我会在详细信息中获得作者姓名。

但是,当我运行我的代码并打印项目时,我看不到作者姓名。我一直在试图弄清楚为什么它没有从数据中显示出来,但我没有看到我的代码有任何问题。

print(searchBooks("9780310709626"))
def getBooks(id):
      url = "https://www.googleapis.com/books/v1/volumes?q=isbn:"
       resp = url(api + id)
       data = json.load(resp)
       print(data["items"])

我的代码输出:



[{'kind': 'books#volume', 'id': 'JEP3sgEACAAJ', 'etag': '92vdEneJ83g', 'selfLink': 'https://www.googleapis.com/books/v1/volumes/JEP3sgEACAAJ', 'volumeInfo': {'title': "The Beginner's Bible", 'subtitle': 'Timeless Bible Stories', 'publisher': 'Zondervan', 'publishedDate': '2005', 'description': 'Retells familiar Bible stories from the Old and New Testaments for children to enjoy.', 'industryIdentifiers': [{'type': 'ISBN_10', 'identifier': '0310709628'}, {'type': 'ISBN_13', 'identifier': '9780310709626'}], 'readingModes': {'text': False, 'image': False}, 'pageCount': 511, 'printType': 'BOOK', 'categories': ['Juvenile Nonfiction'], 'averageRating': 4.5, 'ratingsCount': 2, 'maturityRating': 'NOT_MATURE', 'allowAnonLogging': False, 'contentVersion': 'preview-1.0.0', 'panelizationSummary': {'containsEpubBubbles': False, 'containsImageBubbles': False}, 'imageLinks': {'smallThumbnail': 'http://books.google.com/books/content?id=JEP3sgEACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api', 'thumbnail': 'http://books.google.com/books/content?id=JEP3sgEACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api'}, 'language': 'en', 'previewLink': 'http://books.google.com.tw/books?id=JEP3sgEACAAJ&dq=isbn:9780310709626&hl=&cd=1&source=gbs_api', 'infoLink': 'http://books.google.com.tw/books?id=JEP3sgEACAAJ&dq=isbn:9780310709626&hl=&source=gbs_api', 'canonicalVolumeLink': 'https://books.google.com/books/about/The_Beginner_s_Bible.html?hl=&id=JEP3sgEACAAJ'}, 'saleInfo': {'country': 'TW', 'saleability': 'NOT_FOR_SALE', 'isEbook': False}, 'accessInfo': {'country': 'TW', 'viewability': 'NO_PAGES', 'embeddable': False, 'publicDomain': False, 'textToSpeechPermission': 'ALLOWED', 'epub': {'isAvailable': False}, 'pdf': {'isAvailable': False}, 'webReaderLink': 'http://play.google.com/books/reader?id=JEP3sgEACAAJ&hl=&printsec=frontcover&source=gbs_api', 'accessViewStatus': 'NONE', 'quoteSharingAllowed': False}, 'searchInfo': {'textSnippet': 'Retells familiar Bible stories from the Old and New Testaments for children to enjoy.'}}, {'kind': 'books#volume', 'id': 'ZRgnzQEACAAJ', 'etag': 'RXYM4Rbwx+g', 'selfLink': 'https://www.googleapis.com/books/v1/volumes/ZRgnzQEACAAJ', 'volumeInfo': {'title': "The Beginner's Bible", 'authors': ['Catherine DeVries'], 'publishedDate': '2005', 'industryIdentifiers': [{'type': 'ISBN_10', 'identifier': '0310709628'}, {'type': 'ISBN_13', 'identifier': '9780310709626'}], 'readingModes': {'text': False, 'image': False}, 'pageCount': 511, 'printType': 'BOOK', 'averageRating': 4, 'ratingsCount': 1, 'maturityRating': 'NOT_MATURE', 'allowAnonLogging': False, 'contentVersion': 'preview-1.0.0', 'panelizationSummary': {'containsEpubBubbles': False, 'containsImageBubbles': False}, 'language': 'en', 'previewLink': 'http://books.google.com.tw/books?id=ZRgnzQEACAAJ&dq=isbn:9780310709626&hl=&cd=2&source=gbs_api', 'infoLink': 'http://books.google.com.tw/books?id=ZRgnzQEACAAJ&dq=isbn:9780310709626&hl=&source=gbs_api', 'canonicalVolumeLink': 'https://books.google.com/books/about/The_Beginner_s_Bible.html?hl=&id=ZRgnzQEACAAJ'}, 'saleInfo': {'country': 'TW', 'saleability': 'NOT_FOR_SALE', 'isEbook': False}, 'accessInfo': {'country': 'TW', 'viewability': 'NO_PAGES', 'embeddable': False, 'publicDomain': False, 'textToSpeechPermission': 'ALLOWED', 'epub': {'isAvailable': False}, 'pdf': {'isAvailable': False}, 'webReaderLink': 'http://play.google.com/books/reader?id=ZRgnzQEACAAJ&hl=&printsec=frontcover&source=gbs_api', 'accessViewStatus': 'NONE', 'quoteSharingAllowed': False}}]

【问题讨论】:

  • 作者在回复中。您可以通过data["items"][<INDEX>]["volumeInfo"]["authors"]访问它们
  • 我尝试使用索引 0 并收到错误 KeyError: 'authors' 似乎找不到作者姓名但是当我为另一个 isbn 尝试相同的事情时(例如 9780593433874)我得到了作者姓名
  • 您考虑过使用 Google apis python 客户端库吗?
  • @DaImTo 你能把我链接到客户端库的教程吗?我会试试看
  • 当然,如果我记得书籍 api 不需要授权,让我为您搜索一下 Python samplesDocs,所以我会看看这个示例以了解如何使用 api 密钥@ 987654324@如果您需要帮助更改书籍,请告诉我,我会试一试。

标签: python google-api google-books-api


【解决方案1】:

使用requests 库:

import requests

url = 'https://www.googleapis.com/books/v1/volumes?q=9780310709626'
resp = requests.get(url)
json = resp.json()

print(json['items'][0]['volumeInfo']['authors'])

从响应中您可以看到authors 是一个数组。要访问该数组,您需要执行json['items'][0]['volumeInfo']['authors']

由于items 也是一个数组,这意味着此响应中可能有多个项目。除了硬编码index=0之外,您可能还想编写额外的代码来处理它。

请注意,在这种情况下,您可能不知道响应的架构。 您应该处理意外行为。对于某些特定书籍,可能缺少某些键,json['items'] 可能是一个空数组,甚至items 根本不在响应中。

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2017-11-25
    • 2017-08-09
    相关资源
    最近更新 更多