
116
5 章 Python による Web からのデータの取得
ローカルに格納します。
db_nobel = get_mongo_database('nobel_prize')
col = db_nobel['country_data'] #
国データコレクション
#
すべての
RESTful
な国データを取得する
response = REST_country_request()
# JSON
オブジェクトをコレクションに直接挿入する
col.insert(response.json())
Out:
[ObjectId('5665a1ef26a7110b79e88d49'),
ObjectId('5665a1ef26a7110b79e88d4a'),
...
国データを
MongoDB
コレクションに挿入したので、再び通貨として米ドルを使ってすべての
国を探し出してみましょう。
res = col.find({'currencies':{'$in':['USD']}})
list(res)
Out:
[{u'_id': ObjectId('5665a1ef26a7110b79e88d4d'),
u'alpha2Code': u'AS',
u'alpha3Code': u'ASM',
u'altSpellings': [u'AS',
...
u'currencies': [u'USD'],
u'demonym': u'American Samoan',
u'languages': [u'en', u'sm'],
... ...