Skip to content

Instantly share code, notes, and snippets.

@sureshdsk
Last active March 25, 2019 15:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sureshdsk/9ea4a4337c411c1e4dff to your computer and use it in GitHub Desktop.
Save sureshdsk/9ea4a4337c411c1e4dff to your computer and use it in GitHub Desktop.
Get number of likes of a facebook page using graph api in python
# http://www.idiotinside.com/2015/02/13/get-number-of-likes-of-a-facebook-page-using-graph-api-in-python/
import urllib2
import json
def get_page_data(page_id,access_token):
api_endpoint = "https://graph.facebook.com/v2.4/"
fb_graph_url = api_endpoint+page_id+"?fields=id,name,likes,link&access_token="+access_token
try:
api_request = urllib2.Request(fb_graph_url)
api_response = urllib2.urlopen(api_request)
try:
return json.loads(api_response.read())
except (ValueError, KeyError, TypeError):
return "JSON error"
except IOError, e:
if hasattr(e, 'code'):
return e.code
elif hasattr(e, 'reason'):
return e.reason
page_id = "idiotinside" # username or id
token = "TOKEN" # Access Token
page_data = get_page_data(page_id,token)
print "Page Name:"+ page_data['name']
print "Likes:"+ str(page_data['likes'])
print "Link:"+ page_data['link']
@kiranbulbule
Copy link

i don't understand this. can please explain this? i'm a web designer and now new in developing.

@sureshdsk
Copy link
Author

This is a python program to get facebook page likes programmatically with facebook graph api.

@warreee
Copy link

warreee commented May 8, 2016

Which version of python are you using, it doesn't seem to work for me:
print( "Likes:"+ str(page_data['likes']))
TypeError: 'int' object has no attribute 'getitem'

@bhagyashreeborate
Copy link

This is not giving likes attribute.
only returning id, name, and link

@jimmyVadako
Copy link

Im getting this:

id "303858136365695"
name "Idiot Inside"
likes  
data  
0  
name "Google"
id "104958162837"
paging  
cursors  
before "MTA0OTU4MTYyODM3"
after "MTA0OTU4MTYyODM3"
link "https://www.facebook.com/idiotinside/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment