我并不想进入SQL主场迎战NoSQL的争论。另外,我并不完全了解您的项目以向您提供建议。但似乎你有SQL的一些经验,而且你想:
- 探索JSON看到你可以在你的应用程序中使用什么
- 搞清楚合适的数据库架构
- 解析json并将它们插入到刚创建的数据库中。
我还没有搜索,但也许有由这个http请求提供的信息coursera的文档。你可以用它来指导你的模型开发。
如果不是,或者如果您觉得要跳入数据并凭经验找出模型,好消息是requests.json()会自动将json内容解码为字典。
为了探讨这个字典,你可以使用dict.keys()方法
>>> r.json().keys() # returns the following line:
dict_keys(['unis', 'insts', 'cats', 'topics', 'courses'])
做的是递归得到一个什么样的每个节点下的感觉。如果你点击列表,然后检查几个这些列表。这些列表可能会翻译成sql世界中的行。如果列表包含字典,那么它会让你知道字段名称是什么。如果进一步,这个列表里面的类型的字典嵌套类型的字典,这可能表明关系
例如,
>>> r.json()['unis'].keys() # gives me the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'keys'
所以,我想,
>>> pp.pprint(r.json()['unis'][0]) # which gave me the first record
{'abbr_name': 'Stanford',
'banner': 'https://coursera-university-assets.s3.amazonaws.com/73/a47990ea7c11e3b00589d092602f0d/Stanford-University-Banner-LRG.jpg',
'class_logo': 'https://coursera-university-assets.s3.amazonaws.com/21/9a0294e2bf773901afbfcb5ef47d97/Stanford_Coursera-200x48_RedText_BG.png',
'description': 'The Leland Stanford Junior University, commonly referred to '
'as Stanford University or Stanford, is an American private '
'research university located in Stanford, California on an '
'8,180-acre (3,310 ha) campus near Palo Alto, California, '
'United States.',
'display': True,
'favicon': 'https://coursera-university-assets.s3.amazonaws.com/dc/581cda352d067023dcdcc0d9efd36e/favicon-stanford.ico',
'home_link': 'http://online.stanford.edu/',
'id': 1,
'landing_page_banner': 'https://coursera-university-assets.s3.amazonaws.com/6f/75dd30dd5911e38988193a0e8ad8fe/Stanford_Coursera-200x48_RedText_BG.jpg',
'location': 'Palo Alto, CA, United States',
'location_city': 'Palo Alto',
'location_country': 'US',
'location_lat': 37.4418834,
'location_lng': -122.14301949999998,
'location_state': 'CA',
'logo': 'https://coursera-university-assets.s3.amazonaws.com/d8/4c69670e0826e42c6cd80b4a02b9a2/stanford.png',
'name': 'Stanford University',
'partner_type': 1,
'primary_color': '#8C1515',
'rectangular_logo_svg': 'https://coursera-university-assets.s3.amazonaws.com/d6/cb68d0d09b11e3a575e17d6a22968b/SUSig_StnfrdOnly.svg',
'short_name': 'stanford',
'square_logo': 'https://coursera-university-assets.s3.amazonaws.com/e3/cebbb0d0a311e39b31794df7e5d956/Coursera-SUSig_StnfrdUStack_SQ.png',
'square_logo_source': 'https://coursera-university-assets.s3.amazonaws.com/e2/c49eb0d0a311e3ad37254033038522/Coursera-SUSig_StnfrdUStack_SQ.png',
'square_logo_svg': 'https://coursera-university-assets.s3.amazonaws.com/e0/0dbc10d0a311e3ad37254033038522/Coursera-SUSig_StnfrdUStack_SQ.svg',
'website': '',
'website_facebook': '',
'website_twitter': '',
'website_youtube': ''}
从这里开始,天真,我会创建一个名为coursera_unis的表,并将该行代码返回以下字段:
>>> r.json()['unis'][0].keys()
dict_keys(['website_facebook', 'location', 'website_twitter', 'square_logo', 'favicon', 'id', 'website', 'location_lng', 'logo', 'location_lat', 'partner_type', 'short_name', 'website_youtube', 'square_logo_svg', 'banner', 'primary_color', 'location_country', 'rectangular_logo_svg', 'square_logo_source', 'name', 'landing_page_banner', 'display', 'home_link', 'description', 'abbr_name', 'location_city', 'location_state', 'class_logo'])
然后,下一步将是插入数据。 It's already answered in this SO thread for MySQL。其他数据库后端也有类似的选项,所以它不应该太难。
太宽泛的一个问题。你已经解析了它,并且有上千种方法来保存它。 – tdelaney 2014-12-06 18:43:48
做一些研究:了解不同类型的NoSQL数据库,将它们的特性与您的应用需要进行比较,选择一个并学习如何使用它与Python。 – Jesper 2014-12-06 23:18:47