Last active
March 25, 2019 15:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'] | |
This is a python program to get facebook page likes programmatically with facebook graph api.
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'
This is not giving likes attribute.
only returning id, name, and 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
i don't understand this. can please explain this? i'm a web designer and now new in developing.