PRAW 将 subreddit 中评论超过 3 条的 reddit 用户添加到列表中

PRAW Adding reddit users with 3+ comments in a subreddit to a list

我目前有一个使用 PRAW 编写的 Python reddit 机器人,它从特定的 subreddit 获取所有评论,查看他们的作者并找出该作者是否在 subreddit 中至少有 3 条评论。如果他们有 3 条以上的评论,那么他们将被添加到批准 post 提交的文本文件中。 我的代码目前 "works" 但老实说,它太糟糕了,我什至不确定如何。实现我的目标的更好方法是什么? 我目前拥有的:

def get_approved_posters(reddit):

   subreddit = reddit.subreddit('Enter_Subreddit_Here')
   subreddit_comments = subreddit.comments(limit=1000)
   dictionary_of_posters = {}
   unique_posters = {}
   commentCount = 1
   duplicate_check = False
   unique_authors_file = open("unique_posters.txt", "w")

   print("Obtaining comments...")
   for comment in subreddit_comments:
      dictionary_of_posters[commentCount] = str(comment.author)
      for key, value in dictionary_of_posters.items():
        if value not in unique_posters.values():
            unique_posters[key] = value
    for key, value in unique_posters.items():
        if key >= 3:
            commentCount += 1
        if duplicate_check is not True:
            commentCount += 1
            print("Adding author to dictionary of posters...")
            unique_posters[commentCount] = str(comment.author)
            print("Author added to dictionary of posters.")
            if commentCount >= 3:
                duplicate_check = True

   for x in unique_posters:
      unique_authors_file.write(str(unique_posters[x]) + '\n')

   total_comments = open("total_comments.txt", "w")
   total_comments.write(str(dictionary_of_posters))

   unique_authors_file.close()
   total_comments.close()

   unique_authors_file = open("unique_posters.txt", "r+")
   total_comments = open("total_comments.txt", "r")
   data = total_comments.read()
   approved_list = unique_authors_file.read().split('\n')
   print(approved_list)
   approved_posters = open("approved_posters.txt", "w")
   for username in approved_list:
      count = data.count(username)
      if(count >= 3):
        approved_posters.write(username + '\n')
      print("Count for " + username + " is " + str(count))

   approved_posters.close()
   unique_authors_file.close()
   total_comments.close()

也许只是我今天早上反应迟钝,但我正在努力 follow/understand 您对 commentCount 和 unique_posters 的使用。其实应该是我吧

我会像你一样从 subreddit 获取所有评论,并且对于每条评论,执行以下操作:

for comment in subreddit_comments:
    try:
        dictionary_of_posters[comment.author] += 1
    except KeyError:
        dictionary_of_posters[comment.author] = 1

for username, comment_count in dictionary_of_posters.items():
    if comment_count >= 3:
        approved_authors.append(username)

此方法利用了字典不能有两个相同键值的事实。这样,您就不必进行重复检查或其他任何操作。如果它让你感觉好些,你可以去 list(set(approved_authors)) 并且这将摆脱任何杂散的重复。