程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

通过YouTube的DataAPI获取YouTube数据(基本步骤)

发布于2019-08-06 11:20     阅读(1631)     评论(0)     点赞(5)     收藏(1)


声明:本文章仅用于学习及编程研究使用。

一,准备工作

1.登陆谷歌账号,访问谷歌api,如下图:(因为我已有项目所以,最上方栏显示了我的一个项目)

 

2.点击       (此处下拉箭头),在弹出框中点击“新建项目”:

 

3.启动API和服务

点击“启动API和服务”,在新加载的页面下拉找到YouTube的相关项,如下图:

这里我们选择第一个。点击,跳转到如下界面:点击启用。

依次点击左上角, 继而在左侧栏点击

点击“创建凭据”,在弹出框选择“API密匙”

选择,“限制密匙”,在新界面选择“API限制”,保存此处生成的API密匙!!!

在“API限制”下拉菜单中选择,点击确定即可。

 

二,开始写程序

此处参考官方文档说明:“https://developers.google.com/youtube/v3/code_samples/点我打开”;

模拟实现:“https://developers.google.com/apis-explorer/#p/youtube/v3/点我打开

GitHub中所有的样本代码示例,包含各种语言版本。“https://github.com/youtube/api-samples点击打开

---------------------------------------------------------------------------------------------------------------------------

下面的代码是根据“https://developers.google.com/youtube/v3/code_samples/python#search_by_keyword点击打开处的代码修改而来:(此处使用的是Python,该链接有其他代码示例)

下面代码中,根据自己的api密匙及代理进行对应替换。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import socks
import socket

DEVELOPER_KEY = '把你申请到的api密匙填写到这里' 
YOUTUBE_API_SERVICE_NAME = 'youtube'
YOUTUBE_API_VERSION = 'v3'


def youtube_search(options):
    socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, "此处写你的代理IP", 此处写你的代理端口号)
    socket.socket = socks.socksocket

    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
                    developerKey=DEVELOPER_KEY)
    # Call the search.list method to retrieve results matching the specified
    # query term.
    search_response = youtube.search().list(
        q=options['q'],
        part='id,snippet',
        maxResults=options['max_results']
        ).execute()

    videos = []
    channels = []
    playlists = []

    # Add each result to the appropriate list, and then display the lists of
    # matching videos, channels, and playlists.
    for search_result in search_response.get('items', []):
        if search_result['id']['kind'] == 'youtube#video':
          videos.append('%s (%s)' % (search_result['snippet']['title'],
                                     search_result['id']['videoId']))
        elif search_result['id']['kind'] == 'youtube#channel':
          channels.append('%s (%s)' % (search_result['snippet']['title'],
                                       search_result['id']['channelId']))
        elif search_result['id']['kind'] == 'youtube#playlist':
          playlists.append('%s (%s)' % (search_result['snippet']['title'],
                                        search_result['id']['playlistId']))

    print 'Videos:\n', '\n'.join(videos), '\n'
    print 'Channels:\n', '\n'.join(channels), '\n'
    print 'Playlists:\n', '\n'.join(playlists), '\n'


if __name__ == '__main__':
    args = {'q': 'boating|sailing –fishing', 'max_results': 20}
    try:
        youtube_search(args)
    except HttpError, e:
        print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content)

注:

socks包不能直接pip安装,具体参考 点击打开 https://blog.csdn.net/qq_27378621/article/details/81012561;

程序中的代理IP及代理端口,分别对应你的翻墙工具的IP及端口号。

其他必要的安装(python):

pip install --upgrade google-api-python-client
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2

 



所属网站分类: 技术文章 > 博客

作者:雷神归来

链接:https://www.pythonheidong.com/blog/article/8299/c3257c7cb7c88d9c38d4/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)