Skip to content

Instantly share code, notes, and snippets.

@HH0718
Created November 18, 2022 23:19
Show Gist options
  • Save HH0718/3a23293ebb40aa8d8e3fd50978991c55 to your computer and use it in GitHub Desktop.
Save HH0718/3a23293ebb40aa8d8e3fd50978991c55 to your computer and use it in GitHub Desktop.
Method to find keywords in title and comments of post.
reddit_username=""
password=""
client_id=""
client_secret=""
user_agent="The best thing ever created by me!"
from praw.models import MoreComments
from dotenv import load_dotenv
import os
import praw
load_dotenv()
reddit = praw.Reddit(
client_id=os.getenv('client_id'),
client_secret=os.getenv('client_secret'),
password=os.getenv('password'),
user_agent=os.getenv('user_agent'),
username=os.getenv('reddit_username')
)
submmission_list = []
def get_subreddit_comment_by_keyword(subreddit_name: str, keyword: str) -> None:
subreddits = reddit.subreddit(subreddit_name).hot(limit=2000)
for submission in subreddits:
if keyword in submission.title.lower():
submmission_list.append(submission)
def get_comment_by_keyword(submission_id: str, keyword: str) -> None:
submission = reddit.submission(submission_id)
print('\n' * 5 + "Post Title: " +submission.title + '\n' + '*' * 50)
for comment in submission.comments:
if isinstance(comment, MoreComments):
continue
if keyword in comment.body:
print(comment.body)
if __name__ == "__main__":
keyword = "twitter"
get_subreddit_comment_by_keyword('news', keyword)
for sub in submmission_list:
get_comment_by_keyword(sub.id, keyword)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment