Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环浏览子字符串的所有注释_Python_Reddit_Praw - Fatal编程技术网

Python 循环浏览子字符串的所有注释

Python 循环浏览子字符串的所有注释,python,reddit,praw,Python,Reddit,Praw,我正在编写一个程序,它将检查包含艺术家姓名的字符串列表,并将其与Reddit提交的所有评论进行比较。它在找到一个匹配项后停止,或者根本不工作(即使使用简单的测试字符串),你能告诉我这个bug吗?包括不包括身份验证的Reddit部分 submission = reddit.submission(id='75lnoo') # Topic about Eminem, lots of mentions of him submission.comments.replace_more(limit=0) #

我正在编写一个程序,它将检查包含艺术家姓名的字符串列表,并将其与Reddit提交的所有评论进行比较。它在找到一个匹配项后停止,或者根本不工作(即使使用简单的测试字符串),你能告诉我这个bug吗?包括不包括身份验证的Reddit部分

submission = reddit.submission(id='75lnoo') # Topic about Eminem, lots of mentions of him
submission.comments.replace_more(limit=0)  # Stores all the comments
comments = submission.comments.list()
artists_list = ['Eminem', 'Drake'] # Sample list
for comment in comments:
    for artist in artists_list:
        if artist.lower() in comment.body.lower():
            print(comment.permalink() + ' - ' + artist)
当应该有很多匹配项时,只会打印一件东西

/r/Music/comments/75lnoo/eminem_rips_donald_trump_in_bet_hip_hop_awards/do894hp-eminem


刚刚在我的机器上本地运行了代码,我在
Eminem
上得到了很多结果,而在
Drake
上没有得到任何结果。我的猜测是,因为这让我一开始就感到失望,所以它花了一段时间才得到第一个结果之后的第二个结果。可能是你提前终止了计划,以为所有结果都打印出来了

这里有一个直接复制粘贴:

 import praw

 reddit = praw.Reddit(client_id = '',
                 client_secret= '',
                 user_agent= '',
                 username = '',
                 password = '')

submission = reddit.submission(id='75lnoo')
submission.comments.replace_more(limit=0)  # Stores all the comments
comments = submission.comments.list()
artists_list = ['Eminem', 'Drake'] # Sample list
print(artists_list)
for comment in comments:
for artist in artists_list:
    if artist.lower() in comment.body.lower():
        print(comment.permalink() + ' - ' + artist)

请修复缩进。缩进已修复。哦,好的,我相信我的代码的问题是试图用
artists\u list=filter(None,artists\u list)
过滤艺术家列表中的所有空白,如果没有它,匹配很好,只需要找到另一种过滤方法。啊,我明白了,filter()的第一个参数需要一个返回True或False的函数,然后将其应用于列表,即第二个参数。函数的输出是使函数返回True的元素列表。