📘
Python web crawler note
  • Introduction
  • 1. 環境安裝與爬蟲的基本
  • 1.1 環境安裝
  • 1.2 IDE設定
  • 1.3 一隻很原始的爬蟲
  • 1.4 幫爬蟲裝煞車
  • 2. 用BeautifuSoup來分析網頁
  • 2.1 BeautifulSoup範例 - 1
  • 2.2 BeautifulSoup說明
  • 2.3 BeautifulSoup範例 - 2
  • 2.4 加入Regular Expression
  • 2.5 Dcard今日十大熱門文章
  • 3. 更多實際的應用
  • 3.1 PTT八卦版今日熱門文章
  • 3.2 Yahoo奇摩電影本週新片
  • 3.3 蘋果日報/自由時報今日焦點
  • 3.4 Google Finance 個股資訊
  • 3.5 Yahoo奇摩字典
  • 4. 基於API的爬蟲
  • 4.1 八卦版鄉民從哪來?
  • 4.2 Facebook Graph API
  • 4.3 imdb電影資訊查詢
  • 4.4 Google Finance API
  • 4.5 台灣證券交易所API
  • 5. 資料儲存
  • 5.1 痴漢爬蟲(PTT表特版下載器)
  • 5.2 儲存成CSV檔案
  • 5.3 儲存至SQLite
  • 6. 不同編碼/文件類型的爬蟲
  • 6.1 非UTF-8編碼的文件
  • 6.2 XML文件
  • 7. 比價爬蟲
  • 7.1 momo購物網爬蟲
  • 7.2 PChome 24h API爬蟲
  • 7.3 比價圖表程式
  • 8. 處理POST請求/登入頁面
  • 8.1 空氣品質監測網
  • 9. 動態網頁爬蟲
  • 9.1 台銀法拍屋資訊查詢
  • 10. 自然語言處理
  • 10.1 歌詞頻率與歌詞雲
Powered by GitBook
On this page

Was this helpful?

4.2 Facebook Graph API

Previous4.1 八卦版鄉民從哪來?Next4.3 imdb電影資訊查詢

Last updated 5 years ago

Was this helpful?

要使用FB Graph API, 要先取得自己的token, 可以到這個網站去申請:

點選"取得token" -> "取得用戶存取token" -> 勾選你想讓這個token可以取得的資訊 -> 得到token

把這個token記著, 等等程式裡面要用(ACCESS_TOKEN).

import requests


# To obtain the access token, go to https://developers.facebook.com/tools/explorer.
ACCESS_TOKEN = ''


def get_my_friends():
    url = 'https://graph.facebook.com/v2.9/me?fields=id,name,friends&access_token={}'.format(ACCESS_TOKEN)
    data = requests.get(url).json()
    print('My ID: ' + data['id'])
    print('My name: ' + data['name'])
    print('Total friends: ', data['friends']['summary']['total_count'], 'friends.')


def get_page_post(page_id):
    url = 'https://graph.facebook.com/v2.9/{0}/posts?access_token={1}'.format(page_id, ACCESS_TOKEN)
    data = requests.get(url).json()
    print('There are ', len(data['data']), ' posts on the fans page.')
    print('The latest post time is: ', data['data'][0]['created_time'])
    print('Content:', data['data'][0]['message'])


def main():
    get_my_friends()
    get_page_post(1707015819625206)


if __name__ == '__main__':
    main()

輸出結果:

My ID: XXX
My name: Carl Lu
Total friends:  XX friends.
There are  19  posts on the fans page.
The latest post time is:  2017-05-19T19:39:06+0000
Content: 哇忘記說了~ PyCon Taiwan報名截止了 >< 大家都報名了嗎?

PyCon Taiwan 是台灣 Python 愛好者的年會,每年可以看到大家用 Python 做了哪些好玩有趣的專案,或是相關技術分享。今年的議程不意外地涵蓋許多機器學習、數據分析、聊天機器人的應用。而跟爬蟲直接相關的講題或應用有 "比美麗的湯更美麗:pyquery",以及 "用Python成為網路投資王" 兩場,官方網站如下:
https://tw.pycon.org/2017/zh-hant/

沒有報名到的朋友也別擔心,屆時官方粉絲頁可能會有直播。我們也會參加~ 之後會跟大家分享心得,敬請期待!

P.S. 去年我們的講師 Jun-Wei Lin 參加 PyConTW 2016 的心得可以參考這裡
http://blog.castman.net/programming/2016/06/05/pycontw2016.html

Process finished with exit code 0

原始碼

https://developers.facebook.com/tools/explorer
點我