我想搜索所有YouTube频道包含关键字'投资'无论是在他们的YouTube频道标题或描述,保持一些频道变量,并将它们存储在一个dataframe.我使用的是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个通道,带有所需的关键字“investment”),但是很少有手动检查让我知道这绝对不是一个全面的列表。例如,它不提供具有+200k用户的this YT信道。
恐怕我错过了很多(重要的)频道。是API的问题吗?我的代码?
提前感谢大家,
1条答案
按热度按时间6ovsh4lw1#
我认为这是一个使用API v3无法解决的问题。
我也有同样的问题;我试着减少采样
published date
,但搜索的结果总是在550-590左右。比如说;我试着找一天和一个月的视频,结果是一样的。