“在Praw bot中输入错误:不可用类型:'list'”

时间:2016-03-21 11:08:21

标签: python-3.x praw

为Reddit bot运行以下代码会在代码下方列出错误消息,任何人都可以帮我解决此错误吗?我尝试了几件事,无法在Google上找到答案。提前致谢。 D_035

#Bot

import praw
import time
import re
import os

r = praw.Reddit(user_agent = "Bot")
r.login()
cache = []



def run_bot():
    subreddit = r.get_subreddit("broap")
    comments = subreddit.get_comments(limit=25)
    for comment in comments:
        comment_text = comment.body.lower()
        author = comment.author
        url = comment.link_id
        msg = "User {} has tagged you in a post!".format(author)
        words = filter( lambda x:x.startswith('//'), comment_text.split())
        user = words[2:]  
        if comment.id not in cache and words:
            r.user.send_message(user ,msg)
            cache.add(comment.id)




while True:
    run_bot()
    time.sleep(5)

运行后提供的错误消息:

raceback (most recent call last):
  File "Test.py", line 32, in <module>
    run_bot()
  File "Test.py", line 25, in run_bot
    r.user.send_message(user ,msg)
  File "/usr/local/lib/python2.7/dist-packages/praw/decorators.py", line 60, in wrapped
    return function(self.reddit_session, self, *args, **kwargs)
  File "<decorator-gen-144>", line 2, in send_message
  File "/usr/local/lib/python2.7/dist-packages/praw/decorators.py", line 271, in wrap
    return function(*args, **kwargs)
  File "<decorator-gen-143>", line 2, in send_message
  File "/usr/local/lib/python2.7/dist-packages/praw/decorators.py", line 177, in require_captcha
    return function(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/praw/__init__.py", line 2555, in send_message
    retry_on_error=False)
  File "<decorator-gen-8>", line 2, in request_json
  File "/usr/local/lib/python2.7/dist-packages/praw/decorators.py", line 116, in raise_api_exceptions
    return_value = function(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/praw/__init__.py", line 620, in request_json
    retry_on_error=retry_on_error)
  File "/usr/local/lib/python2.7/dist-packages/praw/__init__.py", line 451, in _request
    response = handle_redirect()
  File "/usr/local/lib/python2.7/dist-packages/praw/__init__.py", line 432, in handle_redirect
    verify=self.http.validate_certs, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/praw/handlers.py", line 137, in wrapped
    if _cache_key in cls.cache:
TypeError: unhashable type: 'list'

1 个答案:

答案 0 :(得分:0)

r.user.send_message()并不期望列表作为第一个参数。从文档中看,它看起来像是一个字符串(类似于用户名)。

您构建user的方式:

    words = filter( lambda x:x.startswith('//'), comment_text.split())
    user = words[2:]

使其成为一个列表(过滤器返回一个可迭代的,然后[2:]返回此可迭代的&#34;部分&#34;;&#39;源&#39;是一个列表(split的输出( )),它返回一个列表),因此错误。

为了纠正您的功能,您必须更改构建user的方式。不知道如何帮助你,因为我不知道你到底想做什么。

编辑: 现在你告诉了一点,我会这样:

def run_bot():
    subreddit = r.get_subreddit("broap")
    comments = subreddit.get_comments(limit=25)
    for comment in comments:
        comment_text = comment.body.lower()
        author = comment.author
        url = comment.link_id
        words = filter( lambda x:x.startswith('//'), comment_text.split())
        users_list = [ w[2:] for w in words ]
        for u in users_list:
            if comment.id not in cache and words:
                r.user.send_message(u, 
        "User {a} has tagged you in a post!".format(a=author))
                cache.append(comment.id)

这应该处理评论中找到的任何//用户名。 (你怎么确定用户名确实存在?这不是问题吗?(例如,如果我写&#34;虚拟评论只是为了尝试// invalid_username&#34;,会发生什么?)

其他:这与您的问题无直接关系,但这一行:

    if comment.id not in cache and words:

可能无法执行您想要的操作(仅当comment.id不在缓存中时才为True,如果单词不是空列表,则不会检查comment.id是否属于单词)。