简体   繁体   English

从Facebook API(Python,JSON)中提取注释

[英]Pull comments from Facebook API (Python, JSON)

I want to pull all comments from all posts the last 24 hours using the Facebook API. 我想使用Facebook API在过去24小时内从所有帖子中提取所有评论。 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. 目前,我只能从帖子的特定数据范围中提取信息,因为Facebook API仅允许在帖子下使用“自”和“直到”。 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. 因此,举例来说,就目前的代码而言,我无法从4月份发布的帖子中提取今天的评论。 Has anyone been able to pull comments from all posts in the last 24 hours without including the posts? 有没有人能够在过去24小时内从所有帖子中提取评论而没有包含这些帖子? 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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