gpt4 book ai didi

python - 如何访问从 google books API python 返回的 JSON 对象的值?

转载 作者:行者123 更新时间:2023-12-01 05:43:00 25 4
gpt4 key购买 nike

我正在从 python 脚本访问 Google Books API,并且需要在获得的 JSON 对象中获取特定值。例如,如果您遵循以下步骤:

https://www.googleapis.com/books/v1/volumes?q=9781607055389

您将看到我尝试使用的 JSON 对象。我需要获取 description 键中包含的书籍描述。我在使用 urllib2 获取后使用 json.load 加载它。

我已尝试执行以下操作但无济于事:

a = json.load(urllib2.urlopen('https://www.googleapis.com/books/v1/volumes?q=9781607055389'))
print a.items[0].description

最佳答案

description 在另一本字典中,您忘记了:

>>> print a['items'][0]['volumeInfo']['description']
Photo tutorials show stitching in action for 50+ free-motion quilting designs to create modern quilts with classic style! Popular blogger and designer, Natalia Bonner, illustrates her instructions with detailed photos that make it easier to get beautiful results on your home sewing machine. Learn how to quilt all-over, as filler, on borders, and on individual blocks...using loops and swirls, feathers and flames, flowers and vines, pebbles and more! Includes tips for choosing batting and thread, layering and basting, starting and stopping, and prepping your machine are included. After you've practiced, show off your new skills with six geometric quilt projects.

您访问字典值的意图不是您在 python 中所做的。您可以使用 get() 函数,或者执行我所做的操作。 .items 起作用的原因是内置函数 .items(),它返回字典的结构。

关于python - 如何访问从 google books API python 返回的 JSON 对象的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17008110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com