【发布时间】:2018-03-30 11:40:30
【问题描述】:
我一直在尝试获取给定频道 ID 的所有视频。但我没有收到所有视频
代码我试图检索频道的所有视频:
api_key = API_KEY
base_video_url = 'https://www.youtube.com/watch?v='
base_search_url = 'https://www.googleapis.com/youtube/v3/search?'
raw_url = 'https://www.googleapis.com/youtube/v3/' \
'channels?key={}&forUsername={}&part=id'
def getChannelID(username):
''' returns the channel ID '''
r=requests.get(raw_url.format(api_key,username))
json=r.json()
print(json['items'][0]['id'])
return json['items'][0]['id']
def getchannelVideos():
''' returns list of all videos of a given channel '''
chanId=getChannelID('tseries')
first_url = base_search_url + \
'order=date&part=snippet&channelId={}&maxResults=50&key={}'\
.format(chanId,api_key)
video_links = []
url = first_url
while True:
inp = requests.get(url)
resp = inp.json()
for i in resp['items']:
if i['id']['kind'] == "youtube#video":
video_links.append(base_video_url + i['id']['videoId'])
try:
next_page_token = resp['nextPageToken']
url = first_url + '&pageToken={}'.format(next_page_token)
except:
break
print('working') #used this to count repetitions of while loop
return video_links
这里给定的频道是T-Series,到目前为止有 11,537 个视频 [click to see the image of the channel showing the count ]但是我只收到了589个视频
我用这条线数了数。 while 循环的迭代次数会做
print('working')
为此,我观察到 while 循环在 19 次迭代后结束(我在许多通道上尝试过,但同样在重复)
这是我提供的最后一次(第 19 次迭代)Json 数据
{'etag': "cbz3lIQ2N25AfwNr-BdxUVxJ_QY/7SEM6nSU4tBD7ZsR5Abt5L-uqAE",
'items': [],
'kind': 'youtube#searchListResponse',
'pageInfo': {'resultsPerPage': 50, 'totalResults': 15008},
'prevPageToken': 'CLYHEAE',
'regionCode': 'IN'}
虽然 totalResults 是 15008,为什么 API 不提供 nextpageID ??
【问题讨论】:
-
AFAIK 在列出频道对象的统计信息时,通过 videoCount 键值检索频道的上传视频计数。您的实施中可能存在的问题:您的 GET 中的 maxResults=50
-
@BoboDarph
maxResults=50是在 API 端实现的限制
标签: python python-3.x api youtube-api youtube-data-api