想要将每个迭代的数据存储在一个数组中

时间:2017-01-11 10:03:41

标签: python facebook-graph-api facepy

import json
import time
import urllib
from pymongo import MongoClient
from facepy import GraphAPI

access_token = 'EAACEdEose0cBAKrXodSbgJM32aihWYeFwnm3X8i2CPB7Pn4L2fujnNDc49pGwuV4pqccHxsX2ooudEVRGRAGe7mDZAB0mlZCGFO12cW78klPGjVeanTaJjwZCuNek8BsQRisUeUh7CijjOyTReVybj0EeZAZBBpf0Y099uE9pBgZDZD'
graph = GraphAPI(access_token,version= '2.8')

def counts():
    posts= "no likes"
    client = MongoClient()
    db = client.fb
    collection = db.check
    query = db.posts.find()
    for q in query:
            liker_id = "null"
            liker_name = "null"
            likes = "no likes in the post"
            id = q['post_id']
            response=graph.get(id+'?fields=likes.summary(true),comments.summary(true),shares')
            if 'likes' in response:
                for d in response['likes']['data']:
                     liker_id = d['id']
                     liker_name = d['name']
                     for i in liker_id:
                          posts = {"id":[liker_id]}
                post = {"post_id":id,"likes":posts}
                print post
counts()

{'post_id': u'256015257837672_1017155801723610', 'likes': {'id': [u'905803702763261']}}
{'post_id': u'256015257837672_1016685905103933', 'likes': {'id': [u'808269765904912']}}

我只在输出中获得最后的LIKER_ID,但我希望将所有LIKER_ID存储在那些喜欢的部分中 请帮助我,谢谢你

1 个答案:

答案 0 :(得分:1)

可以通过将帖子附加为数组来完成,这样就可以获得数组中的所有数据

posts = []  
tmp = {"likerId":liker_id,"likername":liker_name}
posts.append(tmp)