简体   繁体   English

如何使用 Python 获取 Facebook 上帖子的点赞数?

[英]How to get the number of likes for a post on Facebook using Python?

I try to get the number of likes and comments to a post on the Facebook page of an organization.我尝试获取组织 Facebook 页面上帖子的点赞数和评论数。 I have the Python codes for the count of shares work just fine, but not for likes and comments.我有用于共享计数的 Python 代码工作正常,但不适用于喜欢和评论。 Any suggestion?有什么建议吗?

import urllib
import json
import sys
import os

accessToken = 'TOKENVALUE'  #INSERT YOUR ACCESS TOKEN
userId = sys.argv[1]          
limit=100

# Read my likes as a json object
url='https://graph.facebook.com/'+userId+'/posts?access_token='+accessToken +'&limit='+str(limit)
data = json.load(urllib.urlopen(url))
id=0

print str(id)

for item in data['data']:
time=item['created_time'][11:19]
date=item['created_time'][5:10]
year=item['created_time'][0:4]
if 'shares' in item:
    num_share=item['shares']['count']
else:
    num_share=0
if 'likes' in item:
            num_like=item['likes']['count']
else:
            num_like=0


id+=1

print str(id)+'\t'+ time.encode('utf-8')+'\t'+date.encode('utf-8')+'\t'+year.encode('utf-8')+'\t'+ str(num_share)+'\t'+str(num_like)

For getting likes and comments on a post, you would need to make separate call per post.要获得对帖子的喜欢和评论,您需要为每个帖子单独拨打电话。 These calls will give you the desired results.这些调用将为您提供所需的结果。 (look at the 'summary' field in the JSON response) (查看 JSON 响应中的“摘要”字段)

/{POST_ID}/likes?summary=1 /{POST_ID}/likes?summary=1

/{POST_ID}/comments?summary=1 /{POST_ID}/comments?summary=1

Ofcourse you will need to add your access token and all that.当然,您需要添加访问令牌等等。

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

相关问题 如何在Python中使用Facebook图形API获得Facebook朋友的喜欢 - How to get likes of facebook friends using facebook graph api in python 使用Python + Facebook Graph API计算在Facebook帖子上的喜欢数 - Count likes on a Facebook post using Python+Facebook Graph API 我可以在Python中使用facebook sdk获得喜欢,分享和评论的帖子总数吗? - Can I get the total count of a post likes, shares and comments using facebook sdk in Python? Facebook Selenium 怎么获得点赞数 - Facebook Selenium How can I get the number of likes 如何在Python中使用facebook SDK获取facebook帖子的所有评论? - How to get all comments of a facebook post using facebook SDK in Python? 我如何找到像使用Python urllib2和漂亮的汤类库的网页一样的tweets / facebook数量? - How can I find number of tweets/facebook likes any web page had using Python urllib2 and beautiful soup libraries? 如何在Django中检查用户的Facebook点赞/帖子 - How to check user facebook likes/post in django 在Python Social Auth中获得Facebook用户的赞 - Get Facebook user likes in Python Social Auth 从 post instagram python 中获得喜欢 - Get likes from post instagram python 使用 Python 中的 Graph API 将 Facebook 页面的点赞数返回到我的 .html 页面 - Returning the number of likes of a facebook page using Graph API in Python to my .html page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM