I'm using Python to access Facebook pages and retrieve information. When I try to retrieve the list of users who likes an item (ex: a post, a picture), i get only a partial list. I think this is due to paging limit. I know there is a way to change the paging limit in Facebook Graph:
https://graph.facebook.com/[node]/likes?limit=1000
but I do not know how to implement the 'limit' property in Python code.
The code I use to gather the likes is:
import facebook
graph = facebook.GraphAPI(token)
profile = graph.get_object(page_id)
likes = str(profile.get('likes'))
the above will get you the number of likes to the particular page. You can't get the list of fans of particular page. However you can check the if a particular user likes a page .
check the following page for all other common things that can be done with facebook graph api
https://developers.facebook.com/docs/graph-api/common-scenarios
Related
I am using tweepy with python (flask) for a small project. I want to fetch users against a query from twitter. Currently I am using cursor object and it returns me tweets. But I want to fetch users instead of tweets.
I think what I am getting right now via cursor is the top results so I want the results in People tab instead of Top tab.
Any idea how should I do that?
Screenshot is attached for what I want!
https://i.stack.imgur.com/0zGUm.png
I found this in tweepy documentation.
After basic twitter auth:
twitterAuth = twitterClient()
api = twitterAuth.get_twitter_client_api()
api.search_users(q, 20, 1)
Here q is your search term, 20 is the number of results per page (max 20 allowed) and 1 indicates the page number from where you want to collect the users. For example if you want to fetch users from 1st page it will be 1 else any other page number.
Hope this helps everyone!
For a project at university I need to extract data such as posts and reviews from same Facebook pages. Everything was fine couple of months ago but now to get data from pages you need the Public Page Content Access.
In order to get my app reviewed I need to add:
A platform where I'd use the app
A screencast that shows "how a person sees this feature used in your app"
An explanation of how I'd be using Page Public Content Access to enhance the experience of my app.
Privacy Policy URL
As a student who just needs to extract some data for an exam I don't have any website/platform where I'd use the app. I'm using the Facebook Graph API on Python.
I looked on this website for a Privacy Policy Generator but I don't have any website nor mobile apps where I'd use the API...
Is there some way for my situation to extract data by API without this requirements or it's better for me to find other solutions, such as web scraping?
To be able to extract data from Facebook using a python code you need to register as a developer on Facebook and then have an access token. Here are the steps for it.
Go to link developers.facebook.com, create an account there. Go to
link developers.facebook.com/tools/explorer. Go to “My apps” drop
down in the top right corner and select “add a new app”. Choose a
display name and a category and then “Create App ID”. Again get back
to the same link developers.facebook.com/tools/explorer. You will see
“Graph API Explorer” below “My Apps” in the top right corner. From
“Graph API Explorer” drop down, select your app. Then, select “Get
Token”. From this drop down, select “Get User Access Token”. Select
permissions from the menu that appears and then select “Get Access
Token.” Go to link developers.facebook.com/tools/accesstoken. Select
“Debug” corresponding to “User Token”. Go to “Extend Token Access”.
This will ensure that your token does not expire every two hours.
Python Code to Access Facebook Public Data:
Go to link https://developers.facebook.com/docs/graph-api if want to collect data on anything that is available publicly. See https://developers.facebook.com/docs/graph-api/reference/v2.7/. From this documentation, choose any field you want from which you want to extract data such as “groups” or “pages” etc. Go to examples of codes after having selected these and then select “facebook graph api” and you will get hints on how to extract information. This blog is primarily on getting events data.
First of all, import ‘urllib3’, ‘facebook’, ‘requests’ if they are already available. If not, download these libraries. Define a variable token and set its value to what you got above as “User Access Token”.
token= ‘aiufniqaefncqiuhfencioaeusKJBNfljabicnlkjshniuwnscslkjjndfi’
Getting list of Events:
Now to find information on events for any search term say “Poetry” and limiting those events’ number to 10000:
graph = facebook.GraphAPI(access_token=token, version = 2.7)
events = graph.request(‘/search?q=Poetry&type=event&limit=10000’)
This will give a dictionary of all the events that have been created on Facebook and has string “Poetry” in its name. To get the list of events, do:
eventList = events[‘data’]
Extracting all information for a event from the list of events extracted above:
Get the EventID of the first event in the list by
eventid = eventList[1][‘id’]
For this EventID, get all information and set few variables which will be used later by:
event1=graph.get_object(id=eventid,fields=’attending_count,can_guests_invite,category,cover,declined_count,description,end_time,guest_list_enabled,interested_count,is_canceled,is_page_owned,is_viewer_admin,maybe_count,noreply_count,owner,parent_group,place,ticket_uri,timezone,type,updated_time’)
attenderscount = event1[‘attending_count’]
declinerscount = event1[‘declined_count’]
interestedcount = event1[‘interested_count’]
maybecount = event1[‘maybe_count’]
noreplycount = event1[‘noreply_count’]
Getting the list of all those who are attending an event and converting the response into json format:
attenders = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/attending?
access_token="+token+”&limit=”+str(attenderscount))
attenders_json = attenders.json()
Getting the admins of the event:
admins = requests.get(“https://graph.facebook.com/v2.7/"+eventid+"/admins?
access_token="+token)
admins_json = admins.json()
And similarly you can extract other information such as photos/videos/feed of that event if you want.
Go to https://developers.facebook.com/docs/graph-api/reference/event/ and see “Edges” part in the documentation.
Is there a way to scrape Facebook comments and IDs from a Facebook page like nytimes or the guardian for analytical purposes !?
For scraping, the quick answer is no. Use the API. I know this question is for Python, but if you use R, there is the Rfacebook package, which has the functions getPage() and getPost(). A combination of these (i.e. get the page and then loop through the post ids to get the comments with getPost() to get comments and the IDs of the commentators) should get you what you want. Apologies, I don't know if there is anything similar for Python.
for using their API, you'll need to "verify" your app to get access to their "pages_read_user_content" or "Page Public Content Access"
at first using the API you might "GET" the page id / page post id / the permalink to the post in the page your own but to scrape the comments with API you'll need to verify a business account.
I'm facing problem like this. I used tweepy to collect +10000 tweets, i use nltk naive-bayes classification and filtered the tweets into +5000.
I want to generate a graph of user friendship from that classified 5000 tweet. The problem is that I am able to check it with tweepy.api.show_frienship(), but it takes so much and much time and sometime ended up with endless ratelimit error.
is there any way i can check the friendship more eficiently?
I don't know much about the limits with Tweepy, but you can always write a basic web scraper with urllib and BeautifulSoup to do so.
You could take a website such as www.doesfollow.com which accomplishes what you are trying to do. (not sure about request limits with this page, but there are dozens of other websites that do the same thing) This website is interesting because the url is super simple.
For example, in order to check if Google and Twitter are "friends" on Twitter, the link is simply www.doesfollow.com/google/twitter.
This would make it very easy for you to run through the users as you can just append the users to the url such as 'www.doesfollow.com/'+ user1 + '/' + user2
The results page of doesfollow has this tag if the users are friends on Twitter:
<div class="yup">yup</div>,
and this tag if the users are not friends on Twitter:
<div class="nope">nope</div>
So you could parse the page source code and search to find which of those tags exist to determine if the users are friends on Twitter.
This might not be the way that you wanted to approach the problem, but it's a possibility. I'm not entirely sure how to approach the graphing part of your question though. I'd have to look into that.
I'm managing a Facebook page in which I'm also analyzing it's insights. We own the page and every post on the page feed(page doesn't allow other users to post). I'm doing an analysis on all of the posts that we've every created.
I've been using {page}/posts edge to get the post ids but found out that it only returns a subset of the data. Then I tried {page}/links and {page}/videos because these are the post types I'm mostly interested in. The video edge works great; it gave me all of the videos ids from the page. However, {page}/links only returned 2 months worth of link ids.
Here is a sample GET I'm using (I'm trying to get the post ids from 10/2014 to 12/2014):
https://graph.facebook.com/v2.2/{actual_page_id}/links?fields=id,created_time&since=1414175236&until=1419445636&access_token=[The_actual_access_token]
But I get an empty result string:
{"data": []}
And when I set the date with in the 2 months frame I can get proper response.
My question is: Is there a way to get ALL of the Facebook page posts ids that we have created? I've tried to set limits and paging but none have worked. Thank you very much for your help.
The Below snippet should solve your issue, It uses Facepy and handles paging on its own.
from facepy import GraphAPI
import json
access = '<access_token>'
graph = GraphAPI(access)
page_id= '<page_id>'
data= graph.get(page_id+ "/posts?fields=id", page=True, retry=5)
data1=[]
for i in data:
data1.append(i)
print data1