0
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 但我想在 LIKES 部分中存储所有 LIKER_ID 请帮助我谢谢

4

1 回答 1

0

它可以简单地通过将帖子附加为数组来完成,这样它将获取数组中的所有数据

posts = []  
tmp = {"likerId":liker_id,"likername":liker_name}
posts.append(tmp)
于 2017-01-12T08:56:22.897 回答