如何使用google api爬取youtube視頻

說明:

目前認爲比較合適的爬蟲思路是,訂閱你要爬的用戶  >  獲得訂閱的列表 > 獲得某個訂閱的視頻列表 > 下載視頻

 

關於google api的使用方式上,建議首先找到需要嘗試使用的api菜單,然後使用API Explorer運行,如果運行的結果達到自己預期,再把代碼拿到本地去運行,這樣整個流程下來就會比較順暢

 

1.首先要有一個google api賬號

https://console.developers.google.com/apis/

2.獲得訂閱的頻道列表

https://developers.google.com/youtube/v3/docs/subscriptions/list

其中mine選擇爲true

curl \
  'https://www.googleapis.com/youtube/v3/subscriptions?part=snippet%2CcontentDetails&mine=true&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

運行結果中,items[0].snippet.resourceId.channelId 即爲頻道id

3.獲得上傳的視頻列表id

https://developers.google.com/youtube/v3/docs/channels/list

其中id即爲上面拿到的channelId

curl \
  'https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC_x5XG1OV2P6uZZ5FSM9Ttw&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

運行結果中,items[0].contentDetails.uploads 即爲播放列表id

4.獲得視頻列表中的視頻

https://developers.google.com/youtube/v3/docs/playlistItems/list

其中playlistId即爲上面獲得播放列表id

curl \
  'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId=UUpRmvjdu3ixew5ahydZ67uA&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

在運行結果中,我們終於能夠看到朝思夢想的視頻id了,items[0].contentDetails.videoId

5.獲得視頻地址

https://www.youtube.com/embed/{videoId}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章