Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments
-
Upload
cosmopolitanvan -
Category
Education
-
view
2.985 -
download
0
description
Transcript of Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments
![Page 1: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/1.jpg)
1
Created by The Curiosity Bits Blog (curiositybits.com)
The codes are provided by Dr. Gregory D. Saxton
Mining Facebook Page on Python
![Page 2: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/2.jpg)
2
What data are available for mining?
• Posts: all posts on a Facebook page• Content, posted time, included URLs, mentioned Facebook friends/pages, etc.
• Comments: comments on the posts• Sender, content, posted time, etc.
• Engagement Indicators (we will cover this in an upcoming tutorial)• The # of likes, shares and comments on a post• The lifespan of a post: the time between sending the post and receiving its
last comment
![Page 3: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/3.jpg)
3
The data are available through Facebook Graph API• Use Facebook Graph API Explorer (
http://developers.facebook.com/tools/explorer) to get an access token.
![Page 4: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/4.jpg)
4
The JSON data will be stored on a SQLite database• Like Twitter data, the raw output from Facebook API is in
JSON format. To see how the JSON output is organized, use JSON Viewer (http://jsonviewer.stack.hu/)
- DOWNLOAD A JSON OUTPUT SAMPLE - (https://drive.google.com/file/d/0Bwwg6GLCW_IPMWlLNjd2NnplM1k/edit?usp=sharing)
![Page 5: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/5.jpg)
5
A JSON will look like this on a web browser…
![Page 6: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/6.jpg)
6
Copy the content of the downloaded JSON sample onto JSON Viewer (http://jsonviewer.stack.hu), click “Viewer”
Download a sample JSON
![Page 7: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/7.jpg)
7
Let’s get down to the Python code…
- DOWNLOAD THE PYTHON CODE - (https://drive.google.com/file/d/0Bwwg6GLCW_IPbmNiQW1EWGotd28/edit?usp=sharing)
![Page 8: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/8.jpg)
8
Let’s walk you through each part of the script…part 1
Download a sample JSON
• These are necessary Python libraries. You need to install them before running the code.
• Not familiar with the installing? Review our previous tutorial on how to install Python libraries (pg.8) http://curiositybits.com/python-for-mining-the-social-web/python-tutorial-mining-twitter-user-profile/
![Page 9: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/9.jpg)
9
Part 2: Create columns in the database for output variables
the name of a Facebook page; useful when you are mining content from multiple pages
Page id, an unique identifier for a Facebook page
The URL to a Facebook page post
An unique identifier for a Facebook page post, formatted as Page ID_Status ID
An unique identifier for a Facebook page post, without page ID in it
The textual content of a post
![Page 10: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/10.jpg)
10
Part 2: Create columns in the database for output variables
When the page post is sent
When the post is retrieved from API
When the last comment is posted.The type of the post – status, link, photo, video, etc.
The included URL(s) to a video
The name of the webpage that the included URL is linking to.
The Included URL(s) to a photo
Description of the webpage that the included URL is linking to.
![Page 11: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/11.jpg)
11
Part 2: Create columns in the database for output variables
The number of mentions in a post
The mentioned page/people in a post
![Page 12: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/12.jpg)
12
Part 2: Create columns in the database for output variables
The entire JSON raw output, including information not parsed to the existing columns
Comments (including sender, content and posted time) on the first page
Comments on the second page and beyond
![Page 13: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/13.jpg)
13
• The columns created here correspond to the output variables returned by Facebook Graph API. See the definitions of all output variables: https://developers.facebook.com/docs/graph-api/reference/post
![Page 14: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/14.jpg)
14
Part 2: the parsed data look like this in a SQLite Database Browser
Not familiar with SQLite Database Browser? Review our first tutorial (page. 10) at http://curiositybits.com/python-for-mining-the-social-web/python-tutorial-mining-twitter-user-profile/
![Page 15: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/15.jpg)
15
Part 2: the parsed data look like this in a SQLite Database Browser– continued.
Not familiar with SQLite Database Browser? Review our first tutorial (page. 10) at http://curiositybits.com/python-for-mining-the-social-web/python-tutorial-mining-twitter-user-profile/
![Page 16: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/16.jpg)
16
Not familiar with SQLite Database Browser? Review our first tutorial (page. 10) at http://curiositybits.com/python-for-mining-the-social-web/python-tutorial-mining-twitter-user-profile/
Part 2: the parsed data look like this in a SQLite Database Browser– continued.
![Page 17: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/17.jpg)
17
Not familiar with SQLite Database Browser? Review our first tutorial (page. 10) at http://curiositybits.com/python-for-mining-the-social-web/python-tutorial-mining-twitter-user-profile/
Part 2: the parsed data look like this in a SQLite Database Browser– continued.
![Page 18: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/18.jpg)
18
Click to see all comments. Individual comments are separated by the symbol ***
Part 2: the parsed data look like this in a SQLite Database Browser– continued.
![Page 19: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/19.jpg)
19
Q: Why are some columns entirely blank?A: We have created more columns than we needed for this round of data-mining. The additional columns created are for the next iteration of mining through which we will get Facebook engagement indicators.
![Page 20: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/20.jpg)
20
Part 3: Set up the access token in the Python script
Paste your own access token here.
![Page 21: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/21.jpg)
21
Part 4: Set up the SQLite database in the Python script
Name your own database. The database will be saved to the same folder as your Python script.
Or try a complete file path, if you want to save the database in a different folder.
![Page 22: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/22.jpg)
22
Part 3: tell Python which Facebook pages to look for.
The numbers here are Facebook page IDs, wrapped in ‘’, and separated by commas. Each Facebook page will have an unique page ID, which can be found in the page’s URL.
![Page 23: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/23.jpg)
23
You can also use page name if the page name consists of only one word.
Part 3: tell Python which Facebook pages to look for.
![Page 24: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/24.jpg)
24
BUT! Here is a catch:
if a page name contains multiple words, It is recommended that you use numerical page id instead. You can find the numerical page id in the URL - the string of numbers after page name in the URL. (e.g. https://www.facebook.com/pages/Spot-Coffee-Elmwood/316579834919)
Part 3: tell Python which Facebook pages to look for.
![Page 25: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/25.jpg)
25
Part 4: run the scriptHit RUN and you will see Anaconda showing the progress of the data mining.
Not familiar with Anaconda? Review our previous tutorial (pg. 3) at
http://curiositybits.com/python-for-mining-the-social-web/python-tutorial-mining-twitter-user-profile/
![Page 26: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/26.jpg)
26
Lastly, a caveat….
If you encounter an error when running the script, a database (though incomplete) may have been created. You will need to delete the database file and re-run the script, or change the database file name in the script before the second run.
This script does not work on a preexisting database file.
![Page 27: Curiosity Bits Python Tutorial: Mining Facebook Fan Page - getting posts and comments](https://reader035.fdocuments.net/reader035/viewer/2022062703/554f631eb4c905c8088b4b97/html5/thumbnails/27.jpg)
27
COMING UP NEXT…
GET FACEBOOK ENGAGEMENT INDICATORSThe # of likes, shares and comments to the posts, and the
lifespan of a post
Stay tuned to our Curiosity Bites Blog (curiositybits.com)