使用youtube.search().list时获取相同的youtube频道ID



我使用Youtube api获得10个随机Youtube频道id并将它们放入列表中,因此我创建了一个函数来返回此列表:


def youtube_search():
all_data = []
search_response = youtube.search().list(
part='snippet',
maxResults=10,
location=None,
locationRadius=None,
regionCode = 'TN',
)
response = search_response.execute() 
for i in range(len(response['items'])):

data = response['items'][i]['snippet']['channelId']
all_data.append(data)

return all_data

问题是,它返回给我相同的id 10次,正好是2个不同的id(一个id 2次,另一个8次):

channel_data = youtube_search()
channel_data
['UC6UL29enLNe4mqwTfAyeNuw',
'UC6UL29enLNe4mqwTfAyeNuw',
'UCvR2R7j218tzejtTsb_X6Rw',
'UCvR2R7j218tzejtTsb_X6Rw',
'UCvR2R7j218tzejtTsb_X6Rw',
'UCvR2R7j218tzejtTsb_X6Rw',
'UCvR2R7j218tzejtTsb_X6Rw',
'UCvR2R7j218tzejtTsb_X6Rw',
'UCvR2R7j218tzejtTsb_X6Rw',
'UCvR2R7j218tzejtTsb_X6Rw']

怎么了?为什么我得到相同的id,而不是完全不同?

为了从Search.listAPI端点获取仅通道项,有以下请求参数:

type(字符串)

type参数限制搜索查询只能检索特定类型的资源。以逗号分隔的资源类型列表。默认为"video,channel,playlist"。

可接受的值为:

  • channel
  • playlist
  • video
因此,将您的API调用更改为:
search_response = youtube.search().list(
type='channel',
part='snippet',
maxResults=10,
location=None,
locationRadius=None,
regionCode = 'TN',
)

和您将返回的items数组将只包含指向YouTube频道的项。