简体   繁体   中英

Pull comments from Facebook API (Python, JSON)

I want to pull all comments from all posts the last 24 hours using the Facebook API. Currently, I can only pull from a certain data range of posts as the Facebook API only allows "since" and "until" to be used under posts. I can't seem to use those parameters for comments. So for example, with my code currently, I cannot pull today's comments from a post that was posted in April. Has anyone been able to pull comments from all posts in the last 24 hours without including the posts? This is my code so far:

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)

No worries I came up with the code that solves everything for me:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM