发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!