python gdata youtube 评论提要无法检索超过 150 条评论

标签 python youtube gdata

我正在使用 python 的 gdata 模块来提取 YouTube 视频的评论,但我遇到了一些问题,它适用于相对不太受欢迎的视频(没有很多评论的视频),但任何具有更多评论的视频评论,会给我返回一个错误的请求错误。

gdata.service.RequestError: {'status': 400, 'body': 'Invalid value for parameter: video-id', 'reason': 'Bad Request'}

这是我的功能:

def getComments(client, video_id):
    comment_feed = client.GetYouTubeVideoCommentFeed(video_id=video_id)
    while comment_feed is not None:
        for comment in comment_feed.entry:
             yield comment
        next_link = comment_feed.GetNextLink()
        if next_link is None:
             comment_feed = None
        else:
             comment_feed = client.GetYouTubeVideoCommentFeed(next_link.href)

有人知道如何解决这个问题吗?

编辑:

所以我决定尝试另一种方法,这就是我得到的:

from gdata.youtube import service

comment_feed_url = "http://gdata.youtube.com/feeds/api/videos/%s/comments?max-results=50"
USERNAME = ''
PASSWORD = ''

''' Get the comment feed of a video given a video_id'''
def WriteCommentFeed(video_id):
    client = service.YouTubeService()
    client.ClientLogin(USERNAME, PASSWORD)
    url = comment_feed_url % video_id
    comment_feed = client.GetYouTubeVideoCommentFeed(uri=url)
    allComments = []
    while comment_feed:

        for comment_entry in comment_feed.entry:
            allComments.append(comment_entry.content.text)

        print len(allComments)
        print comment_feed.GetNextLink().href
        comment_feed = client.Query(comment_feed.GetNextLink().href)

if __name__ == "__main__":
    WriteCommentFeed("5DdzE4k31fM")

它似乎在 150 条评论时中断,因此在查询第 200 条评论时,我收到此错误:

'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 414, 'body': '<!DOCTYPE html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">\n  <title>Error 414 (Request-URI Too Large)!!1</title>\n  <style>\n    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}\n  </style>\n  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n  <p><b>414.</b> <ins>That\xe2\x80\x99s an error.</ins>\n  <p>The requested URL <code>/feeds/api/videos/5DdzE4k31fM/comments</code>... is too large to process.  <ins>That\xe2\x80\x99s all we know.</ins>\n', 'reason': 'Request-URI Too Large'}

有人知道为什么吗?

最佳答案

当尝试对包含大量评论的视频的评论进行分页时,API 会生成 length > 2048 个字符的分页链接。因此,您最终会看到 HTTP 414 错误页面。

要使其正常工作:

假设您提出以下请求:

http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments

那么您的查询字符串参数之一应为:

orderby=已发布

例如:

“https://gdata.youtube.com/feeds/api/videos/”+ _videoId +“/comments?orderby=published&max-results=50”

这样,API 根据时间而不是相关性检索结果,这可能就是它生成的起始 token 要短得多的原因。

希望这有帮助。

关于python gdata youtube 评论提要无法检索超过 150 条评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826218/

相关文章:

python - Tkinter OptionMenu 标题在第二个 GUI 窗口中消失

youtube - 如何预加载嵌入式YouTube播放列表?

html - 具有Player API(v2)的YouTube HTML5 Video Player后备

python - 抓取youtube用户信息

api - Haskell REST/GDATA API 库

python - 我如何在其内部重复这个 numpy 数组?

python - 运行 spinglass 社区检测算法后,如何获取 python igraph 中每个社区的成员列表

java - Gdata API 博主示例

python - python 中循环( native 与子范围)中列表处理的执行是否有差异

javascript - YouTube JavaScript Player API seekTo()不可靠