如何将facepy数据存储到mongodb中?

时间:2015-11-05 11:22:09

标签: python json mongodb facebook-graph-api facepy

我正在使用这种方法获取comments page数据。它工作正常,但我需要将数据转储到MongoDB中。使用此方法插入数据但作为单个文档。我想存储每个注释应该有一个单独的文档,其中包含我从API获得的信息。

from facepy import GraphAPI
import json
import pymongo
import json
connection = pymongo.MongoClient("mongodb://localhost")

facebook = connection.facebook
commen = facebook.comments
access = ''
#message
graph = GraphAPI(access)
page_id= 'micromaxinfo'
datas= graph.get(page_id+'/posts?fields=comments,created_time', page=True, retry=5)

posts=[]


for data in datas:
    print data

    commen.insert(data)
    break

MongoDB中存储的输出:

{
            "created_time" : "2015-11-04T08:04:14+0000",
            "id" : "120735417936636_1090909150919253",
            "comments" : {
                "paging" : {
                    "cursors" : {
                        "after" : "WTI5dGJXVnVkRjlqZFhKemIzSTZNVEE1TVRReE5ESTVOelV6TlRRd05Ub3hORFEyTnpFNU5UTTU=",
                        "before" : "WTI5dGJXVnVkRjlqZFhKemIzSTZNVEE1TURrd09UVTRNRGt4T1RJeE1Eb3hORFEyTmpJME16Z3g="
                    }
                },
                "data" : [
                    {
                        "created_time" : "2015-11-04T08:06:21+0000",
                        "message" : "my favorite mobiles on canvas silver",
                        "from" : {
                            "name" : "Velchamy Alagar",
                            "id" : "828304797279948"
                        },
                        "id" : "1090909130919255_1090909580919210"
                    },
                    {
                        "created_time" : "2015-11-04T08:10:13+0000",
                        "message" : "Micromax mob. मैने कुछ दिन पहले Micromax Bolt D321 mob. खरिद लिया | Bt मेरा मोबा. बहुत गरम होता है Without internate. और internate MB कम समय मेँ ज्यादा खर्च होती है | कोई तो help करो.",
                        "from" : {
                            "name" : "Amit Gangurde",
                            "id" : "1637669796485258"
                        },
                        "id" : "1090909130919255_1090910364252465"
                    },
                    {
                        "created_time" : "2015-11-04T08:10:27+0000",
                        "message" : "Nice phones.",
                        "from" : {
                            "name" : "Nayan Chavda",
                            "id" : "1678393592373659"
                        },
                        "id" : "1090909130919255_1090910400919128"
                    },
                    {
                        "created_time" : "2015-11-04T08:10:54+0000",
                        "message" : "sir micromax bolt a089 mobile ki battery price kitna. #micromax mobile",
                        "from" : {
                            "name" : "Arit Singha Roy",
                            "id" : "848776351903695"
                        },

从技术上讲,我只想存储data字段中的信息:

{
                            "created_time" : "2015-11-04T08:10:54+0000",
                            "message" : "sir micromax bolt a089 mobile ki battery price kitna. #micromax mobile",
                            "from" : {
                                "name" : "Arit Singha Roy",
                                "id" : "848776351903695"
                            }

如何将其导入我的数据库?

1 个答案:

答案 0 :(得分:0)

您可以使用pentaho数据集成开源ETL工具。我用它来存储推文的JSON输出中的特定字段。

从JSON中选择要解析的字段,并在Oracle等中选择输出为csv或table输出。

希望这有帮助

enter image description here

enter image description here