2014-12-06 95 views
-1

我使用的是Public API,它列出了coursera的所有诅咒。 json大约8MB。现在,我的问题是我如何有效地解析json并将内容保存在我的数据库中。解析大JSON - Python

这里是我的代码: -

import requests 
r = requests.get('https://www.coursera.org/maestro/api/topic/list2') # Is getting all the information at once, the standard way of doing it or should I get in chunks? 
print r.json() 

# Now to save the details, should I use a NoSQL DB. 
# I've little experience of using a NoSQL DB, hence for building an app that list all coursera courses, 
# saving data inside a Mongo will be a good choice or not. 

感谢

+0

太宽泛的一个问题。你已经解析了它,并且有上千种方法来保存它。 – tdelaney 2014-12-06 18:43:48

+0

做一些研究:了解不同类型的NoSQL数据库,将它们的特性与您的应用需要进行比较,选择一个并学习如何使用它与Python。 – Jesper 2014-12-06 23:18:47

回答

2

我并不想进入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。其他数据库后端也有类似的选项,所以它不应该太难。

+0

感谢Haleemur的深入回应。我正在考虑使用NoSQL DB,原因有两个。 1)在一个对象中有太多的信息,这可能有用也可能没有用,因此我正在考虑使用NoSQL DB,因为它可以通过制作一个行和一列来存储信息。 2)我在NoSQL方面经验不足,并且在某种程度上我会掌握相同的知识。 – PythonEnthusiast 2014-12-06 21:55:06

+0

我想听听你对此的看法。我的目标是通过利用coursera的公共API来制作一个具有列表视图和详细视图的网络应用程序。参考网址: - https://www.coursera。组织/课程 – PythonEnthusiast 2014-12-06 21:56:28

+0

看起来像一个足够简单的应用程序,从持久层不需要太多。如果您选择适当的NoSQL存储或RDBMS存储,它将会很好地工作。建立关系型数据库可能会有更多的工作要做,但是你仍然可以在一个下午开始运行。如果你想用这个来学习你不太熟悉的东西,那就去做吧! – 2014-12-06 23:26:02