通过 YouTube API 获取 YouTube 播放列表信息 (Python)

3 投票
1 回答
3449 浏览
提问于 2025-04-17 04:39

我在使用Python的YouTube API获取播放列表中视频标题时遇到了一些问题。我已经正确配置了环境,当我从API文档中复制示例代码时,它能正常工作,能获取我添加的播放列表ID的视频标题。但是,当我尝试使用另一个播放列表的ID时,就出现了错误。

这是我写的一些代码:(在这个例子中,我试图获取这里视频的标题)

import gdata.youtube
import gdata.youtube.service

yt_service = gdata.youtube.service.YouTubeService()
yt_service.ssl = True

# a typical playlist URI
playlist_uri = "http://gdata.youtube.com/feeds/api/playlists/PLCD939C4D974A5815"

playlist_video_feed = yt_service.GetYouTubePlaylistVideoFeed(playlist_uri)

# iterate through the feed as you would with any other
for playlist_video_entry in playlist_video_feed.entry:
    print playlist_video_entry.title.text

这是我遇到的错误:

RequestError: {'status': 400, 'body': 'Invalid playlist id', 'reason': 'Bad Request'}

我对此感到很沮丧,希望能得到一些帮助。谢谢!

1 个回答

4

在你的请求网址中去掉 PL

playlist_uri = "http://gdata.youtube.com/feeds/api/playlists/CD939C4D974A5815"

我不太清楚 为什么 YouTube 需要这个格式,但确实是这样的。

你也可以直接对你的字符串使用 .replace('playlists/PL', 'playlists/') 来替换。

撰写回答