循环遍历子字符串的所有注释

时间:2017-10-15 20:55:51

标签: python reddit praw

我正在编写一个程序,它将覆盖包含艺术家姓名的字符串列表,并将其与Reddit提交的所有评论进行比较。它在找到一个匹配后停止或根本不起作用(即使是简单的测试字符串),你能指出我的错误吗?包括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

1 个答案:

答案 0 :(得分:1)

只是在我的机器上本地运行代码我为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)