149

Background

最新因為工作上的關係需要利用到 Facebook Graph API 來抓取一些FB的資料. 所以寫篇文章記錄一下. 方便以後查詢.

Basic Concept

Facebook資料以一個的"Social Graph"的結構概念存在,簡介中是這樣形容:

  • nodes - basically "things" such as a User, a Photo, a Page, a Comment.
  • edges - the connections between those "things", such as a Page's Photos, or a Photo's Comments
  • fields - info about those "things", such as a person's birthday, or the name of a Page

Facebook for Developers

打開瀏覽器登入 https://developers.facebook.com/ => Tools & Support =>
Graph API Explorer. 會看到以下頁面.

Screen-Shot-2018-01-03-at-4.51.48-PM

Install

$ mkdir fb_crawler
$ cd fb_crawler
$ virtualenv .venv
$ source .venv/bin/activate
$ pip3 install facebook-sdk

Get Self Post Information

crawler.py

import facebook
import requests
import json
from pprint import pprint


TOKEN = $YOUR_TOKEN
HOST = 'https://graph.facebook.com'

page_info = graph.get_object('DoctorKoWJ', field = 'likes')
pprint(page_info)
page_id = page_info['id']
page_name = page_info['name']
posts = graph.get_connections(id = 'DoctorKoWJ', connection_name = 'posts')

for post in posts['data']:

    post_id = post['id']
    created_time = post['created_time']
    message = post['message'].strip()

    url = HOST + '/v2.11/' + post['id'] + '/?fields=likes.limit(0).summary(True), comments.limit(0).summary(True), shares'
    data = {
        'access_token': TOKEN,
        'summary': True,
    }
    res = requests.get(url, data)
    res = json.loads(res.text)

    if 'likes' in res:
        likes = res['likes']['summary'].get('total_count')
    else:
        likes = 0
    if 'shares' in res:
        shares = res['shares'].get('count')
    else:
        shares = 0

    if 'comments' in res:
        comments = res['comments']['summary'].get('total_count')
    else:
        comments = 0
        
    print(page_id, page_name, post_id, created_time, likes, shares, comments)

稍微解釋一下 code, 基本的 object info 大概只有 id & name 而已. 如果你要 post 的資料必須要透過 get_connections 來取得 (符合基本觀念中的edges), 但裡面的資訊也只有 created_time, id, message 這三種欄位資訊. 所以如果你還想取得像是likes等其他資訊. 這種比較近一步的資訊時你必須還要送一個request給facebook來取得. 這裡應該是facebook sdk有改版了. 所以網路上大部分的code都已經不適用了. 上面的程式範例中我們可以取得 likes, comments, shares的數字.

Token

fb token 通常很短的時間內就會過期. 因次你必須想辦法延長. 那facebook自己有開一個API給大家延長 token. 如下面所示:

https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=[app_id]&client_secret=[app_secret]&fb_exchange_token=[token]

那 app_id, app_secret & token. 應該在facebook devloper tool 的頁面中找得到. 這邊就不細講. 依序填入後把網址貼到瀏覽器上就可以看到一組新的Token了.

Reference