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. I have the Python codes for the count of shares work just fine, but not for likes and comments. Any suggestion?
import urllib import json import sys import os accessToken = 'TOKENVALUE' #INSERT YOUR ACCESS TOKEN userId = sys.argv 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)
Ofcourse you will need to add your access token and all that.