从Facebook API(Python,JSON)中提取评论

时间:2015-08-17 16:04:20

标签: python json facebook facebook-graph-api

我想使用Facebook API在过去24小时内从所有帖子中提取所有评论。目前,我只能从某个数据范围的帖子中提取,因为Facebook API只允许"因为" "直到"在帖子下使用。我似乎无法使用这些参数进行评论。例如,就我目前的代码而言,我无法从4月发布的帖子中获取今天的评论。 有没有人能够在过去24小时内从所有帖子中提取评论而不包含帖子?这是我目前为止的代码:

import facebook
import requests
import json
import urllib
import urllib2
import time

now = 1439769600
thyme = int(time.time())
since = int((thyme - 0.7 * 60 * 1000))

user = 'INSERT USER ID/NUMBER'
access_token = 'INSERT ACCESS TOKEN'
url = ' https://graph.facebook.com/v2.4/' + user + '?fields=posts.until' + '(' + str(now) + ')' + '.since' + '(' + str(since) + ')' + '.limit(100)%7Bcreated_time%2Cmessage%2Ccomments.limit(1000)%7Bcreated_time%2Cmessage%7D%7D&access_token='

html = url + access_token
print html

data = json.load(urllib2.urlopen(html))
with open('here.txt', 'w') as textfile:
    json.dump(data, textfile)

1 个答案:

答案 0 :(得分:0)

不用担心我想出了能够解决所有问题的代码:

import facebook
import requests
import json
import urllib
import urllib2

#def some_action(post):
    #print posts['data']
 #   print(post['created_time'])

access_token = 'ENTER ACCESS TOKEN'
url = 'https://graph.facebook.com/v2.4/ENTER FACEBOOK USERNAME/IDNUMBER FOLLOWED BY ?fields=posts.since(1406851200).limit(50)%7Bcreated_time%2Cmessage%2Ccomments.limit(2000)%7Bcreated_time%2Cmessage%7D%7D&access_token='

html = url + access_token

data = json.load(urllib2.urlopen(html))
with open('penguin.txt', 'w') as textfile:
    json.dump(data, textfile)