说明:
目前认为比较合适的爬虫思路是,订阅你要爬的用户 > 获得订阅的列表 > 获得某个订阅的视频列表 > 下载视频
关于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}