如何使用 Youtube Data API v3 获取 youtube 频道中的所有视频标题?
How to get all videos titles in a youtube channgel using Youtube Data API v3?
我正在使用 Youtube 数据 API v3.
提取 YouTube 频道中所有视频的标题
我关注了 https://developers.google.com/youtube/v3/code_samples/python
中的片段
我在查询 ['statistics']['videoCount']
时得到一个数字
但是当我在 youtube 中搜索实际频道时,它给出了不同的视频数量。
假设我正在尝试 ID 为 - UCeLHszkByNZtPKcaVXOCOQQ
的频道
['statistics']['videoCount']
给出 19
但是,如果我在 youtube 上搜索频道 Post Malone,它有 36 个视频。我哪里错了?
['statistics']['videoCount']
是否真的给出了 YouTube 频道中视频的确切数量?
这是我的代码:
from pprint import pprint
from googleapiclient.discovery import build
import os
YOUTUBE_API_KEY = os.environ.get('YOUTUBE_API_KEY')
youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
lis = ['UCeLHszkByNZtPKcaVXOCOQQ']
for i in lis:
channels_response = youtube.channels().list(part='statistics', id=i).execute()
print(i, channels_response['items'][0]['statistics']['videoCount'])
for i in lis:
channels_response = youtube.channels().list(part='contentDetails', id=i).execute()
for channel in channels_response['items']:
uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
playlistitems_list_request = youtube.playlistItems().list(
playlistId=uploads_list_id,
part="snippet",
maxResults=50
)
while playlistitems_list_request:
playlistitems_list_response = playlistitems_list_request.execute()
for playlist_item in playlistitems_list_response["items"]:
# pprint(playlist_item)
title = playlist_item["snippet"]["title"]
video_id = playlist_item["snippet"]["resourceId"]["videoId"]
print(title, video_id)
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response
)
首先,您要打印给定 YouTube 频道的视频数量 (通过使用其 channel_id
)。
获得 channel_id
后,使用此请求检索以下数据:
- 上传的视频数量(即它的
videoCount
)。
- 包含上传视频的播放列表的
playlistid
。
这是请求:
https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UCeLHszkByNZtPKcaVXOCOQQ&fields=items(contentDetails%2Cid%2Csnippet(country%2Cdescription%2Ctitle)%2Cstatistics%2Cstatus)%2CnextPageToken%2CpageInfo%2CprevPageToken%2CtokenPagination&key={YOUR_API_KEY}
这些是 YouTube 频道的结果:Post Malone
您可以在 Google API Explorer demo:
中测试这些结果
{
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"id": "UCeLHszkByNZtPKcaVXOCOQQ",
"snippet": {
"title": "Post Malone",
"description": "The official Post Malone YouTube Channel.\nwww.postmalone.com"
},
"contentDetails": {
"relatedPlaylists": {
"uploads": "UUeLHszkByNZtPKcaVXOCOQQ",
"watchHistory": "HL",
"watchLater": "WL"
}
},
"statistics": {
"viewCount": "967939106",
"commentCount": "0",
"subscriberCount": "11072809",
"hiddenSubscriberCount": false,
"videoCount": "19"
}
}
]
}
检查这两个值:uploads
和 videoCount
。
如果你输入Post Malone's uploaded videos,你会得到他确实有19个上传的视频(与videoCount
值显示的数量相同).
在你的问题中你说:
However if I search for the channel Post Malone on youtube, it has 36
videos in it. Where am I going wrong?
我不认为你做错了什么,只是你没有完整的频谱。你看,如果你检查它的一些 playlists
,你会看到这 35 个视频对应于这些播放列表:
- Music = 33 个视频。
- More about the artist = 2 个视频。
他的所有 35 个视频都显示在他的 "videos" tab 频道中。
总而言之,这 19 个视频对应于他上传的 19 个视频 (分组在他的 "uploads" 播放列表中)。如果您想检索他的所有视频,您有一个选择是检索 YouTube 频道拥有的所有播放列表。
对于这种情况,这些视频实际上并不在频道中,而是在一个单独的自动生成的 YouTube 频道中,因此造成了混乱。
此代码段可让您从 YouTube 播放列表中提取所有视频标题:
import re
import requests
url = "https://www.your_playlist_url.com"
r = requests.get(url)
code = r.text
titles = re.findall("simpleText":"[^"]*"},"index", code)
for i in titles: print(i[13: -9])`
它只是取 HTML 并过滤掉存储在复杂 javascript object.
中的视频标题
我正在使用 Youtube 数据 API v3.
提取 YouTube 频道中所有视频的标题我关注了 https://developers.google.com/youtube/v3/code_samples/python
中的片段我在查询 ['statistics']['videoCount']
但是当我在 youtube 中搜索实际频道时,它给出了不同的视频数量。
假设我正在尝试 ID 为 - UCeLHszkByNZtPKcaVXOCOQQ
的频道['statistics']['videoCount']
给出 19
但是,如果我在 youtube 上搜索频道 Post Malone,它有 36 个视频。我哪里错了?
['statistics']['videoCount']
是否真的给出了 YouTube 频道中视频的确切数量?
这是我的代码:
from pprint import pprint
from googleapiclient.discovery import build
import os
YOUTUBE_API_KEY = os.environ.get('YOUTUBE_API_KEY')
youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
lis = ['UCeLHszkByNZtPKcaVXOCOQQ']
for i in lis:
channels_response = youtube.channels().list(part='statistics', id=i).execute()
print(i, channels_response['items'][0]['statistics']['videoCount'])
for i in lis:
channels_response = youtube.channels().list(part='contentDetails', id=i).execute()
for channel in channels_response['items']:
uploads_list_id = channel["contentDetails"]["relatedPlaylists"]["uploads"]
playlistitems_list_request = youtube.playlistItems().list(
playlistId=uploads_list_id,
part="snippet",
maxResults=50
)
while playlistitems_list_request:
playlistitems_list_response = playlistitems_list_request.execute()
for playlist_item in playlistitems_list_response["items"]:
# pprint(playlist_item)
title = playlist_item["snippet"]["title"]
video_id = playlist_item["snippet"]["resourceId"]["videoId"]
print(title, video_id)
playlistitems_list_request = youtube.playlistItems().list_next(
playlistitems_list_request, playlistitems_list_response
)
首先,您要打印给定 YouTube 频道的视频数量 (通过使用其 channel_id
)。
获得 channel_id
后,使用此请求检索以下数据:
- 上传的视频数量(即它的
videoCount
)。 - 包含上传视频的播放列表的
playlistid
。
这是请求:
https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UCeLHszkByNZtPKcaVXOCOQQ&fields=items(contentDetails%2Cid%2Csnippet(country%2Cdescription%2Ctitle)%2Cstatistics%2Cstatus)%2CnextPageToken%2CpageInfo%2CprevPageToken%2CtokenPagination&key={YOUR_API_KEY}
这些是 YouTube 频道的结果:Post Malone
您可以在 Google API Explorer demo:
中测试这些结果{
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"id": "UCeLHszkByNZtPKcaVXOCOQQ",
"snippet": {
"title": "Post Malone",
"description": "The official Post Malone YouTube Channel.\nwww.postmalone.com"
},
"contentDetails": {
"relatedPlaylists": {
"uploads": "UUeLHszkByNZtPKcaVXOCOQQ",
"watchHistory": "HL",
"watchLater": "WL"
}
},
"statistics": {
"viewCount": "967939106",
"commentCount": "0",
"subscriberCount": "11072809",
"hiddenSubscriberCount": false,
"videoCount": "19"
}
}
]
}
检查这两个值:uploads
和 videoCount
。
如果你输入Post Malone's uploaded videos,你会得到他确实有19个上传的视频(与videoCount
值显示的数量相同).
在你的问题中你说:
However if I search for the channel Post Malone on youtube, it has 36 videos in it. Where am I going wrong?
我不认为你做错了什么,只是你没有完整的频谱。你看,如果你检查它的一些 playlists
,你会看到这 35 个视频对应于这些播放列表:
- Music = 33 个视频。
- More about the artist = 2 个视频。
他的所有 35 个视频都显示在他的 "videos" tab 频道中。
总而言之,这 19 个视频对应于他上传的 19 个视频 (分组在他的 "uploads" 播放列表中)。如果您想检索他的所有视频,您有一个选择是检索 YouTube 频道拥有的所有播放列表。
对于这种情况,这些视频实际上并不在频道中,而是在一个单独的自动生成的 YouTube 频道中,因此造成了混乱。
此代码段可让您从 YouTube 播放列表中提取所有视频标题:
import re
import requests
url = "https://www.your_playlist_url.com"
r = requests.get(url)
code = r.text
titles = re.findall("simpleText":"[^"]*"},"index", code)
for i in titles: print(i[13: -9])`
它只是取 HTML 并过滤掉存储在复杂 javascript object.
中的视频标题