我使用 Python 版本的 YouTube API 来通过以下 URL 获取 YouTubeVideoFeed 对象:
注意:我已将 USERNAME 替换为我需要关注的帐户。
到目前为止,获取提要、迭代条目、获取玩家 URL、标题和缩略图都非常简单。但现在我想在页面上添加一个“访问 channel ”链接。我不知道如何从提要中获取 channel 的“公共(public)”URL(在本例中为用户的默认 channel )。据我所知,直接存储在 Feed 中的唯一 URL 指向 http://gdata.youtube.com/
,而不是公共(public)网站。
如何链接到基于 Feed 的 channel ?
最佳答案
嗯,如果您想自己构建 URL,youtube.com/user/USERNAME 是一个非常安全的选择,但我认为您想要的是链接 rel='alternate'
您必须从提要中获取链接数组并迭代以找到替代项,然后获取 href
类似于:
client = gdata.youtube.service.YouTubeService()
feed = client.GetYouTubeVideoFeed('http://gdata.youtube.com/feeds/api/users/username/uploads')
for link in feed.link:
if link.rel == 'alternate':
print link.href
输出:
http://www.youtube.com/profile_videos?user=username
最正确的做法是从用户个人资料源中获取“备用”链接,因为从技术上讲,上述 URL 会转到上传的视频,而不是主 channel 页面
feed = client.GetYouTubeUserEntry('http://gdata.youtube.com/feeds/api/users/username')
for link in feed.link:
if link.rel == 'alternate':
print link.href
关于python - 如何使用 YouTube API 从 YouTubeVideoFeed 对象获取公共(public) channel URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/776110/