对于一个大学项目,我试图提取 Facebook 公共页面上的信息。我知道需要许可,但这不是我问题的重点。
我正在编写验证阶段所需的代码,并在我自己的页面上对其进行测试。目前我可以获得所有必要的数据,除了:
每个帖子的各种反应(喜欢、爱、拥抱等)的数量/类型。
帖子类型(包含照片、视频、纯文本、链接等)。
阅读文档,我不知道如何提取该信息。我只能看到我选择的反应,但我可以看到每个帖子的总反应(例如,我可以看到一个帖子有 7 个反应,但它们不分为各种类型)。
最后,我真的不知道如何才能分析帖子的类型。
下面是我写的代码。我知道它不太漂亮,我仍在学习。我仍在测试一些东西,最终所有数据都会放入数据框中。
预先感谢您的关注以及您能给我的任何帮助。
token=""
def export_post_booster(token):
page_id=""
graph = facebook.GraphAPI(token)
posts = graph.request(page_id+'/posts')
count=1
while "paging" in posts:
for post in posts["data"]:
shares = graph.request(post["id"]+"?fields=shares")
reacts = graph.request(post["id"]+"/likes?summary=True")
coms = graph.request(post["id"]+"?fields=comments.summary(true)")
none = graph.request(post["id"]+"?fields=reactions.type(NONE).summary(true)")
like = graph.request(post["id"]+"?fields=reactions.type(LIKE).summary(true)")
love = graph.request(post["id"]+"?fields=reactions.type(LOVE).summary(true)")
wow = graph.request(post["id"]+"?fields=reactions.type(WOW).summary(true)")
haha = graph.request(post["id"]+"?fields=reactions.type(HAHA).summary(true)")
sad = graph.request(post["id"]+"?fields=reactions.type(SAD).summary(true)")
angry = graph.request(post["id"]+"?fields=reactions.type(ANGRY).summary(true)")
coms = graph.request(post["id"]+"?fields=comments.summary(true)")
print("----------------",count,"----------------")
print("time : ",post["created_time"])
print("id :",post["id"],"\n")
if "message" in post:
print("Text Post : ",post["message"])
else:
print("Text Post : NULL")
try:
print("shares :",shares["shares"]["count"])
except:
print("shares : 0")
try:
print("likes : ",reacts["summary"]["total_count"])
except:
print("likes : 0")
try:
print("none : ",none["summary"]["total_count"])
except:
print("none : 0")
try:
print("love : ",love["summary"]["total_count"])
except:
print("love : 0")
try:
print("wow : ",wow["summary"]["total_count"])
except:
print("wow : 0")
try:
print("sad : ",sad["summary"]["total_count"])
except:
print("sad : 0")
try:
print("love : ",love["summary"]["total_count"])
except:
print("love : 0")
try:
print("angry : ",angry["summary"]["total_count"])
except:
print("angry : 0")
for i in range(0, len(coms["comments"]["data"])):
print("><><><")
print("Comment Text: ",i)
print(
{
"id_post": coms["id"],
"data": coms["comments"]["data"][i]["created_time"],
"message": coms["comments"]["data"][i]["message"]
}
)
count=count+1
try:
posts=requests.get(posts["paging"]["next"]).json()
except:
print("end of posts")
break
我得到了总反应类型: https://graph.facebook.com/{postid}?fields=reactions.type({reactiontype}).limit(0).summary(total_count)&access_token={accesstoken} 可用的反应类型有:['LIKE'、'LOVE'、'WOW'、'HAHA'、'ANGRY'、'SAD'] 可能还有更多,如果您输入错误的反应类型,结果 JSON 将包含现有反应类型的列表。
对于我使用的帖子类型: https://graph.facebook.com/{postid}?fields=status_type&access_token={accesstoken}
我是个菜鸟,所以可能有更好的方法来做到这一点,也许请求更少,但这暂时可以完成工作
这部分与您的问题无关,但是您是否已经检索到与该帖子互动的人员列表?