2014-01-21 44 views
2

因此,我正在製作一個小型的reddit機器人,它只是在評論中刮掉了一個術語,但我得到了奇怪的結果。我對python非常陌生,所以這段代碼可能有點混亂和成熟。用python分析PRAW中的評論問題

#! /usr/bin/python 

import praw 

import pprint 

user_agent = ("simple praw script for searching post terms in comments by /u/shadowfire452") 
reddit = praw.Reddit(user_agent = user_agent) 
reddit.login() 
v_fixed = [] 
subreddit = reddit.get_subreddit('politics' and 'worldnews') 

for submission in subreddit.get_hot(limit = 100): 
    title = submission.title 
    if " " in title.lower(): 
     v_fixed.append(title) 
print "The following %d posts might not make much sense ..." % (len(v_fixed)) 
for fixed in v_fixed: 
    print "\t%s" % (fixed) 




flat_comment_generator = praw.helpers.flatten_tree(submission.comments) 

for comment in flat_comment_generator: 
    if "you" in comment.body: 
     a = [] 
     commentz = comment.body 
     a.append(commentz) 
     print comment.body 
     print ("I found %s comments with 'you' in it out of 100 posts") % (len(a)) 
    else: 
      print "I found no comments with 'you' in it" 

當我運行它,我得到:

I found 1 comments with ' ' in it out of 100 posts 
I found no comments with ' ' in it 
I found no comments with ' ' in it 
I found no comments with ' ' in it 
I found no comments with ' ' in it 

顯然,這是一個問題,因爲我得到相互矛盾的答案和5個答覆1個請求。

+0

你知道嗎? – fuzz

回答

1
import praw # simple interface to the reddit API, also handles rate limiting of requests 
import re 
from collections import deque 
from time import sleep 

USERNAME = "" 
PASSWORD = "" 
USERAGENT = "bot/1.0 by USERNAME" 

r = praw.Reddit(USERAGENT) 
r.login(USERNAME, PASSWORD) # necessary if your bot will talk to people 

cache = deque(maxlen = 200) # To make sure we don't duplicate effort 

# Set of words to find in the comment body. 
# I have changed this to a set. 
words = set(["these", "are", "the", "words", "to", "find"]) 

def word_check(comment_body, words): 
    # Will split the comment_body into individual words and check each for membership in words 

    # Split comment into words 
    comment_words = comment_body.split() 

    # Check each word for hot word and return True if found 
    for word in comment_words: 
     if word in words: 
      return True 

    # Return false if no words in words 
    return False 

def bot_action(comment, reply): 
    print "Body:", comment.body 
    print "Found word in:", comment.subreddit.display_name 
    comment.reply(reply) 

# Loop through comments 
running = True 
while running: 
    all = r.get_comments('politics', limit = None) 
    for comment in all: 
     # if comment id exists in cache, break 
     if comment.id in cache: 
      break 
     cache.append(comment.id) # cache already found comment id 
     # execute method for comment body and hotword(s) 
     if word_check(comment.body, words): 
      try: 
       # action the bot to reply 
       bot_action(comment, "Hello world") 
      except KeyboardInterrupt: 
       running = False 
      except praw.errors.APIException, e: 
       print "[ERROR]:", e 
       print "Sleeping for 30 seconds" 
       sleep(30) 
      except Exception, e: # In reality you don't want to just catch everything like this, but this is toy code. 
       print "[ERROR]:", e 
       print "Blindly handling error" 
       continue