Python Web Scraping Part: 7 Facebook SDK

iOSTom
Python + Data Science + Web
4 min readFeb 1, 2020

Over the last few articles I explained how to scrape data from a single site. As I continued down this path I started to see how useful and powerful data really is. In this article I am going to show you how to use the facebook api. By the end of this article you will be able to get tokens, connect to your user and business page and send a facebook post all using python.

Let’s start off by creating a page because I am amusing you have a facebook user profile already created. So head over to your timeline and navigate to the pages on the left hand side.

Pages

Click, “Create Page” for a business and follow the steps.

Create Page
Create business page

If everything goes correctly your business page should be created. Navigate back to your timeline, click on pages and your new business page should be listed.

Business Page

Now that I have both a user profile and a business page I want to be able to post content by using the facebook sdk and python.

Head over to the facebook developer page and create a new app https://developers.facebook.com/. Once you create a new app click on graph API explorer.

If you look on the right hand side you have the ability to create different access tokens. User tokens control your user profile and page tokens control your business page. I ended up creating both.

User Token

Next, create a python script called facebook_client.py and create a variable called page_token to store your access_token. The final thing todo is install the facebook-sdk.

pip install facebook-sdk

Verify that everything is sent up everything correctly. Also instead of app_id it should be page_id.

Instead of APP_ID use your page_id

I did not have a clue what the page_id was or where to find it, some say you can find it on your profile page but the easiest way for me was to look at my facebook url.

If your page url is structured like this: https://www.facebook.com/honeypot-123432423 then your page_id is 123432423. I think I am going to save the facebook api calls for the next article. So, If you learned something click the clapper and stay tuned. If you messed any of my previous articles check them out below.

--

--

iOSTom
Python + Data Science + Web

iOS Developer, Go, Java, C#, Blockchain enthusiast, Data junkie