【问题标题】:Retrieve post of a Facebook page using Facebook API (python)使用 Facebook API (python) 检索 Facebook 页面的帖子
【发布时间】:2015-03-25 03:18:02
【问题描述】:

目前,我正在使用https://github.com/pythonforfacebook/facebook-sdk

我不久前刚学了python。

我需要做什么: 检索特定 Facebook 页面的帖子。

例子:

在 Facebook Graph Explorer API,以 walmart 为例

/v2.2/walmart?fields=posts{message}

结果就是我需要的。

我有什么

以the get_post.py example为例

targetProfile = 'walmart'
graph = facebook.GraphAPI(key)
profile = graph.get_object(targetProfile)
posts = graph.get_connections(profile['id'], 'posts')

print posts['data']

使用有效的访问密钥等运行上述代码后,它似乎打印出消费者的 cmets/post,但我只需要来自 walmart 的帖子。有人能告诉我我应该做什么,或者我做错了什么吗?

提前致谢!

【问题讨论】:

    标签: python facebook facebook-graph-api


    【解决方案1】:

    经过反复试验,我找到了答案。这可能不是最好的解决方案,但它满足我的要求。

    profile = graph.get_object(targetProfile+"/statuses")
    
    Jstr = json.dumps(profile)
    JDict = json.loads(Jstr)
    
       for i in JDict['data']:
           print "message: "+i['message']
    

    【讨论】:

      【解决方案2】:

      下面的 sn-p 应该可以解决您关于检索所有帖子消息的问题,它使用 facepy 并自行处理分页。

      from facepy import GraphAPI
      import json
      
      access = '<access_token>'
      
      graph = GraphAPI(access)
      page_id= '<page_name or page_id>'
      datas= graph.get(page_id+'/posts?fields=message', page=True, retry=5)
      
      posts=[]
      
      for data in datas:
          posts.append(data)
          print posts
      

      【讨论】:

        【解决方案3】:

        profile = graph.get_object(targetProfile+"/statuses") print profile['data'][0]['message']

        这将返回您个人资料的第一条消息,而不使用 JStr 和 JDict var

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-10
          • 1970-01-01
          • 1970-01-01
          • 2014-07-19
          • 2014-01-28
          • 2013-07-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多