我想搜索所有在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个频道带有所需的关键字“投资”),但很少有手动检查让我知道这绝对不是一个全面的列表。例如,它不提供
this
YT频道拥有+20万用户。
恐怕我错过了许多(重要的)渠道。API有问题吗?用我的代码?
提前感谢大家,