【发布时间】:2015-02-02 23:52:03
【问题描述】:
首先,之前对类似问题的“答案”使用了已弃用的 Youtube Api V2。此外,没有一个答案显示您如何使用 Python 来做到这一点。
我的代码是:
def youtube_search(item):
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=item,
part="id,snippet",
maxResults=6
).execute()
videoCount = 0;
title = ""
channelId = ""
channelName = ""
# 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":
videoCount += 1
if videoCount == 1:
snippet = search_result["snippet"]
title = snippet["title"]
channelId = snippet["channelId"]
channelName = snippet["channelTitle"]
# print "Videos:\n", "\n".join(videos), "\n"
print( item, videoCount, title, channelId, channelName )
所以基本上从第一个返回的视频开始,我想知道视频的标题和频道的名称。这是我特别要找的名字。对于这个搜索字符串:
r'''"Joe Keegan" "Pick Myself Up"'''
我的结果是:
('"Joe Keegan" "Pick Myself Up"', 1, u'Pick Myself Up', u'UCThUpINg-JADDOHdkz_4-jw', u'')
我在这里寻找的频道名称是“Various Artists - Topic”。特别是我有一个巨大的列表,并试图消除具有该频道名称的任何内容(很多)。然而,有很多频道 ID 使用该名称。 channelTitle 没有给我我想要的东西。
【问题讨论】:
标签: python youtube-api