"Type error: unhashable type: 'list'" in Praw bot
"Type error: unhashable type: 'list'" in Praw bot
运行 Reddit 机器人的以下代码给出了代码下方列出的错误消息,谁能帮我解决这个错误?我已经尝试了几件事,但无法在 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'
r.user.send_message()
不期望列表作为第一个参数。从文档来看,它似乎需要一个字符串(类似于用户名)。
你的构建方式user
:
words = filter( lambda x:x.startswith('//'), comment_text.split())
user = words[2:]
使它成为一个列表(过滤器 returns 一个可迭代的,然后 [2:] returns 这个可迭代的 "portion" ; 'source' 是一个列表( split() 的输出),它 returns 一个列表),因此出现错误。
为了更正您的功能,您必须更改构建方式 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)
这应该处理在评论中找到的任何//用户名。 (你怎么确定用户名确实存在?这不是问题吗?(例如,如果我写 "A dummy comment just to try //invalid_username",会发生什么?))
其他:这与您的问题没有直接关系,但是这一行:
if comment.id not in cache and words:
可能不会做你想做的事(只有当 comment.id 不在缓存中并且 words 不是空列表 时才会为真,它不会检查如果 comment.id 属于单词)。
运行 Reddit 机器人的以下代码给出了代码下方列出的错误消息,谁能帮我解决这个错误?我已经尝试了几件事,但无法在 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'
r.user.send_message()
不期望列表作为第一个参数。从文档来看,它似乎需要一个字符串(类似于用户名)。
你的构建方式user
:
words = filter( lambda x:x.startswith('//'), comment_text.split())
user = words[2:]
使它成为一个列表(过滤器 returns 一个可迭代的,然后 [2:] returns 这个可迭代的 "portion" ; 'source' 是一个列表( split() 的输出),它 returns 一个列表),因此出现错误。
为了更正您的功能,您必须更改构建方式 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)
这应该处理在评论中找到的任何//用户名。 (你怎么确定用户名确实存在?这不是问题吗?(例如,如果我写 "A dummy comment just to try //invalid_username",会发生什么?))
其他:这与您的问题没有直接关系,但是这一行:
if comment.id not in cache and words:
可能不会做你想做的事(只有当 comment.id 不在缓存中并且 words 不是空列表 时才会为真,它不会检查如果 comment.id 属于单词)。