'CommentHelper' 不可迭代
'CommentHelper' is not iterable
我正在尝试学习如何在 Python 中制作 Reddit 机器人,但收到以下错误消息...
C:\Users\Warbz\Desktop>python redditbot.py
Logging in...
Grabbing subreddit...
Grabbing comments...
Traceback (most recent call last):
File "redditbot.py", line 30, in <module>
run_bot()
File "redditbot.py", line 19, in run_bot
for comment in subredditComments:
TypeError: 'CommentHelper' object is not iterable
我的代码是这样的...
import praw
r = praw.Reddit(client_id="***",
client_secret="***",
username="***",
password="***",
user_agent = "/u/Fazy89")#Description of the script
print("Logging in...")
words_to_match = ['definately', 'defiantly', 'definantly', 'definatly', 'definitly']
cache = []#ID's of comments already replied to
def run_bot():
print("Grabbing subreddit...")
subreddit = r.subreddit("test")#Get SubReddit e.g /r/test
print("Grabbing comments...")
subredditComments = subreddit.comments
for comment in subredditComments:
comment_text = comment.body.lower()#Assign variable to lower case body of text
isMatch = any(string in comment_text for string in words_to_match)
if comment.id not in cache and isMatch:#^If comment has not been added to cache and is a match
print("Match found! Comment ID: " +comment.id)
comment.reply('I think you meant to say "Definitely".')
print("Reply successful!")
cache.append(comment.id)#Add comment ID to cache
print("Comments loop finished, going to sleep...")
while True:
run_bot()
time.sleep(10)
我试过查看 PRAW api,但我不确定到底出了什么问题,有什么帮助吗?
comments
提供了一个CommentHelper的实例,调用时需要使用括号实例化。
for comment in r.subreddit('test').comments(limit=25):
print(comment.author)
我正在尝试学习如何在 Python 中制作 Reddit 机器人,但收到以下错误消息...
C:\Users\Warbz\Desktop>python redditbot.py
Logging in...
Grabbing subreddit...
Grabbing comments...
Traceback (most recent call last):
File "redditbot.py", line 30, in <module>
run_bot()
File "redditbot.py", line 19, in run_bot
for comment in subredditComments:
TypeError: 'CommentHelper' object is not iterable
我的代码是这样的...
import praw
r = praw.Reddit(client_id="***",
client_secret="***",
username="***",
password="***",
user_agent = "/u/Fazy89")#Description of the script
print("Logging in...")
words_to_match = ['definately', 'defiantly', 'definantly', 'definatly', 'definitly']
cache = []#ID's of comments already replied to
def run_bot():
print("Grabbing subreddit...")
subreddit = r.subreddit("test")#Get SubReddit e.g /r/test
print("Grabbing comments...")
subredditComments = subreddit.comments
for comment in subredditComments:
comment_text = comment.body.lower()#Assign variable to lower case body of text
isMatch = any(string in comment_text for string in words_to_match)
if comment.id not in cache and isMatch:#^If comment has not been added to cache and is a match
print("Match found! Comment ID: " +comment.id)
comment.reply('I think you meant to say "Definitely".')
print("Reply successful!")
cache.append(comment.id)#Add comment ID to cache
print("Comments loop finished, going to sleep...")
while True:
run_bot()
time.sleep(10)
我试过查看 PRAW api,但我不确定到底出了什么问题,有什么帮助吗?
comments
提供了一个CommentHelper的实例,调用时需要使用括号实例化。
for comment in r.subreddit('test').comments(limit=25):
print(comment.author)