2014-12-06 119 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