1

我正在使用这种方法来获取数据commentspage它工作正常,但我需要将数据转储到 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"
                            }

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

4

1 回答 1

0

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

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

希望这可以帮助

在此处输入图像描述

在此处输入图像描述

于 2016-03-27T12:25:15.280 回答