1

Python スクリプトから Google Books API にアクセスしています。取得した JSON オブジェクトで特定の値を取得する必要があります。たとえば、これに従うと:

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

使用しようとしている JSON オブジェクトが表示されます。キーに含まれる本の説明を取得する必要がありdescriptionます。を使用json.loadしてフェッチした後にロードするために使用していますurllib2

私は次のことを無駄にしようとしました:

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

2 に答える 2

2

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()

于 2013-06-09T09:12:30.687 に答える
0

以下を使用してみてください。

a['items'][0]['volumeInfo']['description']

注意として、 a.items() と a['items'] は同じものではありません。a.items() は、タプルのリストでキーと値のペアを提供します。

于 2013-06-09T09:12:39.710 に答える