PRAW 4:尝试下载用户评论时出现 400 错误

PRAW 4: 400 ERROR when trying to download a users comments

我正在尝试创建一个 Reddit 机器人,当提到用户名时它会获取用户评论并将其发送到 API 以分析它们和 return 请求但是当我尝试下载它们给了我

prawcore.exceptions.BadRequest: received 400 HTTP response

这是代码:

    if text.startswith('/u/PersonalityInsights'):
        print "Mentioned!"
        print comment
        username = text.split()[1]
        print username
        if username.startswith('/u/'):
            validusername = username[3:]
            print validusername
            global redditor
            redditor = bot.redditor(username)
        else:
            global redditor
            redditor = bot.redditor(username)
        file = codecs.open('userscommentsreddit.txt', 'w+', encoding='utf8')
        for comment in redditor.get_comments(limit=None):
            print comment.body

我使用的方法是:get_comments(limit=None)

在 PRAW 3 中它有效,但在这里它失败了。我尝试将用户名小写,但失败并出现相同的错误。我正在尝试下载评论的帐户是我的:/u/UnknownDeveloper

我尝试降低用户名但出现同样的错误,将 for 循环替换为:for comment in redditor.comments.new(limit=None): 仍然是错误 400

如果您对完整代码感兴趣,这里是 link 到 GitHub gist

Praw 4.1.0 版本

我知道这已经快一年了,很可能你已经继续前进或已经解决了这个问题,但我真的很喜欢做研究和回答 SO 问题所以我们开始吧!:

这里是 400 错误的意思,所以我们在同一页上:

The HTTP 400 Bad Request response status code indicates that the server could not understand the request due to invalid syntax.source

现在进入实际答案:

The method I'm using is: get_comments(limit=None)

in PRAW 3 it worked but here it fails. I tried lowercasing the username but it failed with the same error.

get_comments() 调用已贬值,我假设您根据您也尝试过 comments.new() 函数的事实弄清楚了这一点。

但这根本不重要,因为真正的问题在redditor = bot.redditor(username)。该调用采用原始用户名(没有 /u/),因此您的代码的解决方案是在调用 redditor = bot.redditor(username) 之前从用户名变量中删除 /u/,或者您可以通过以下方式使事情变得更简单使用正则表达式来识别有效的用户名,但仅 return /u/.

之后的部分