- R for Data Science Cookbook
- Yu Wei Chiu (David Chiu)
- 826字
- 2021-07-14 10:51:27
Accessing Facebook data
Social network data is another great source for the user who is interested in exploring and analyzing social interactions. The main difference between social network data and web data is that social network platforms often provide a semi-structured data format (mostly JSON). Thus, one can easily access the data without the need to inspect how the data is structured. In this recipe, we will illustrate how to use rvest
and rson
to read and parse data from Facebook.
Getting ready
In this recipe, you need to prepare your environment with R installed and a computer that can access the Internet.
How to do it…
Perform the following steps to access data from Facebook:
- First, we need to log in to Facebook and access the developer page (https://developers.facebook.com/):
Figure 18: Accessing the Facebook developer page
- Click on Tools & Support, and select Graph API Explorer:
Figure 19: Selecting the Graph API Explorer
- Next, click on Get Token, and choose Get Access Token:
Figure 20: Selecting the Get Access Token
- On the User Data Permissions pane, select user_tagged_places and then click Get Access Token:
Figure 21: Selecting permissions
- Copy the generated access token to the clipboard:
Figure 22: Copying the access token
- Try to access the Facebook API by using
rvest
:> access_token <- '<access_token>' > fb_data <- html(sprintf("https://graph.facebook.com/me/tagged_places?access_token=%s",access_token))
- Install and load the
rjson
package:> install.packages("rjson") > library(rjson)
- Extract the text from
fb_data
and then usefromJSON
to read the JSON data:> fb_json <- fromJSON(fb_data %>% html_text())
- Use
sapply
to extract the name and ID of the place fromfb_json
:> fb_place <- sapply(fb_json$data, function(e){e$place$name}) > fb_id <- sapply(fb_json$data, function(e){e$place$id})
- Last, use
data.frame
to wrap the data:> data.frame(place = fb_place, id = fb_id)
How it works…
In this recipe, we cover how to retrieve social network data through Facebook's Graph API. Unlike scraping web pages, you need to obtain a Facebook access token before making any request for insight information. There are two ways to retrieve the access token: one is to use Facebook's Graph API Explorer, and the other is to create a Facebook application. In this recipe, we illustrate how to use the Graph API Explorer to obtain the access token.
Facebook's Graph API Explorer is where you can craft your requests URL to access Facebook data on your behalf. To access the Explorer page, we first visit Facebook's developer page (https://developers.facebook.com/). The Graph API Explorer page is under the drop-down menu for Tools & Support. After going to the Explorer page, we select Get Access Token from the drop-down menu for Get Token. Subsequently, a tabbed window will appear; you can check access permission to various levels of the application. For example, we can check tagged_places to access the locations we have previously tagged. After we have selected the permissions we require, we can click on Get Access Token to allow Graph API Explorer to access our insight data. After completing these steps, you will see an access token, which is a temporary, short-lived token that you can use to access the Facebook API.
With the access token, we can then access the Facebook API with R. First, we need a HTTP request package. Similar to the web scraping recipe, we can use the rvest
package to make the request. We craft a request URL with the addition of the access_token
(copied from Graph API Explorer) to the Facebook API. From the response, we should receive JSON formatted data. To read the attributes of JSON formatted data, we install and load the RJSON
package. We can then use the fromJSON
function to read the JSON format string extracted from the response.
Lastly, we read places and ID information through the use of the sapply
function, and we can then use data frame to transform extracted information into the data frame. At the end of the recipe, we should see data formatted in the data frame.
There's more…
To learn more about Graph API, read the following official documentation from Facebook: https://developers.facebook.com/docs/reference/api/field_expansion/.
- First, we need to install and load the
Rfacebook
package:> install.packages("Rfacebook") > library(Rfacebook)
- We can then use built-in functions to retrieve data from the user or access similar information with the provision of an access token:
> getUsers("me", "<access_token>")
If you would like to scrape public fan pages without logging in to Facebook every time, you can create a Facebook app to access insight information on behalf of the app:
- To create an authorized app token, log in to the Facebook developer page and click on Add a New Page:
Figure 23: Creating a new app
- You can create a new Facebook app with any name and a valid e-mail ID, providing that it has not already been registered:
Figure 24: Creating a new app ID
- Next, you can copy both the app ID and app secret and craft the access token to
<APP ID>|<APP SECRET>
. You can now use this token to scrape public fan page information with Graph API:Figure 25: Obtaining the app ID and secret
- Similar to Rfacebook, we can then replace the
access_token
with<APP ID>|<APP SECRET>
:> getUsers("me", "<access_token>")
- 編程卓越之道(卷3):軟件工程化
- Practical Internet of Things Security
- Java:Data Science Made Easy
- Learning Laravel 4 Application Development
- 游戲程序設計教程
- 教孩子學編程:C++入門圖解
- Mastering Apache Spark 2.x(Second Edition)
- Getting Started with Greenplum for Big Data Analytics
- 微服務架構深度解析:原理、實踐與進階
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Visual Basic程序設計習題與上機實踐
- JSP程序設計與案例實戰(zhàn)(慕課版)
- Unity 2017 Game AI Programming(Third Edition)
- Redmine Cookbook
- Node.js 6.x Blueprints