我想搜索所有在 YouTube 频道标题或描述中包含关键字“投资”的 YouTube 频道,保留一些频道变量并将它们存储在数据框中。我正在使用 API v3。
我创建了以下 Python 代码(循环不同的页面结果):
def search_channels_with_keyword(youtube, keyword):
# Initialize variables for pagination (prepare the loop over YT 50 results x page)
next_page_token = None
channels = [] # store/append results in this list
while True:
# Search channels with given keyword in title/description
search_response = youtube.search().list(
q=keyword, part='snippet', type='channel', maxResults=50,
pageToken=next_page_token
).execute()
# Process the search results
for search_result in search_response.get('items', []):
channel_id = search_result['id']['channelId']
channel_title = search_result['snippet']['title']
channel_description = search_result['snippet']['description']
channel_thumbnailurl = item['snippet']['thumbnails']['default']['url']
channels.append({ # Append vars in list 'channels'
'channel_id': channel_id,
'channel_title': channel_title,
'channel_description': channel_description,
'channel_thumbnailurl': channel_thumbnailurl
})
# Check if more pages to fetch
next_page_token = search_response.get('nextPageToken')
if not next_page_token:
break # Exit the loop if no more pages
return channels
if __name__ == "__main__":
keyword = 'investment'
channels = search_channels_with_keyword(youtube, keyword)
# Store results in pandas df
df_channels = pd.DataFrame(channels)
df_channels
上面的代码提供了一些不错的输出(584 个频道,带有所需的关键字“投资”),但是很少有手动检查让我知道这绝对不是一个全面的列表。例如,它不提供拥有超过 20 万订阅者的this YT 频道。
我担心我错过了很多(重要的)频道。是API的问题吗?用我的代码?
提前谢谢大家,
我认为使用 API v3 无法解决这个问题...我也有同样的问题,我尝试按发布日期减少采样,但搜索结果总是接近值 550-590。例如,我尝试查找一天和一个月的视频,结果值相同。