gpt4 book ai didi

Python:TypeError: 'Comment' 类型的参数不可迭代

转载 作者:行者123 更新时间:2023-11-28 21:19:41 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的 reddit 机器人,它将进入 subreddit,进入提交,阅读评论,如果评论说“感觉”,它会发布感觉 gif。我得到这个错误:'TypeError: argument of type 'Comment' is not iterable' when trying to do has_feels = 'feels' in comment.

我的代码:

import praw
import time

r = praw.Reddit('Posts Feels gif in response to someone saying feels'
'by: Mjone77')
r.login('Feels_Bot', 'notrealpassword')
already_done = []

feels = ['feels']
while True:
subreddit = r.get_subreddit('bottest')
for submission in subreddit.get_new(limit=10):
#submission = next(submissions)
commentNum = 0
for comment in submission.comments:
print(comment)
print(comment.id)
has_feels = 'feels' in comment
if comment.id not in already_done and has_feels:
#comment.reply('[Relevant](http://i.imgur.com/pXBrf.gif)')
already_done.append(comment.id)
print('Commented')
time.sleep(1800)

错误报告(前两行是代码的打印输出,直到它中断):

The feels are strong
ciafpqn
Traceback (most recent call last):
File "C:\Users\Me\Desktop\My Programs\Feels Bot\Feels Bot\FeelsBot.py", line 18, in <module>
has_feels = 'feels' in comment
TypeError: argument of type 'Comment' is not iterable
sys:1: ResourceWarning: unclosed <socket object at 0x0369D8E8>
C:\Python34\lib\importlib\_bootstrap.py:2150: ImportWarning: sys.meta_path is empty

任何人都知道如何解决这个问题,所以如果评论在其中某处包含“感觉”,它会将 has_feels 设置为 true 吗?

此外,一旦它确实处理了提交中的所有评论,它就会停止,不会进入下一次提交。任何人都知道如何解决这个问题?如果您现在还不知道,请不要费心为我查找任何内容,我可以找到那部分内容。

最佳答案

has_feels = 'feels' in comment

comment 看起来是一个对象,其属性本质上不像 liststring 那样可迭代。

From the praw documentation ,您需要通过 body 属性访问评论的文本:

所以像这样:

has_feels = 'feels' in comment.body

以下是一些关于为什么您当前所做的不起作用的示例:

>>> "x" in "xxyy"  # works (string is iterable)
>>> "x" in ["x", "y"] # works (list is iterable)

>>> class MyClass(): pass
>>> c = MyClass()
>>> "y" in c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'instance' is not iterable

关于Python:TypeError: 'Comment' 类型的参数不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24277305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com