[英]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.