How to check if website post title has certain word in it?

Renato

I'm building one bot for fun and I need some help. I successfully made the part where my bot will automatically reply to comments if there is already a comment containing certain words in it. (I removed all bot info from the beginning including my account info)

import praw
import time

reddit = praw.Reddit(
    client_id = "------------",
    client_secret = "-------------",
    user_agent = "replyBot",
    password = "---------",
    username = "-------")

subreddit = reddit.subreddit("all")


for submission in subreddit.new(limit = 10):
    print("****")
    print(submission.title)

    for comment in submission.comments:
        if hasattr(comment, "body"):
            comment_lower = comment.body.lower()
            if "certain word" in comment_lower:
                print("-------")
                print(submission.title)
                print(comment.body)
                print(comment.author)

                comment.reply("Test reply")
                time.sleep(780)

This code, once executed gives such output which is alright(I randomly put r/love so I can show you its output), in this case, it outputs post title, a comment which includes word "love" in it and author of that comment.

enter image description here

Now I can't figure out how to make bot to do the same thing(prints post title,comment and its author) but if there is certain word in the POST TITLE and not the comments section.

EDIT : This is what I tried

for title_name in submission.title:
    if hasattr(title_name, "title"):
        title_lower = title_name.body.lower()
        if " love " in title_lower:
            print(submission.title)
Stefano Frazzetto

I have never used Reddit SDK, but I suspect the issues with your code are the following:

  1. title doesn't have a body.

  2. You don't need to iterate through the submission's title, as there is only one for a given post.

  3. The issue with checking if the string is contained in the title might be caused by the spaces in your string: your code is going to print the post only if the title contains something like "I love ice-cream", but it won't print it for "Love is a good thing".

Example:

for submission in subreddit.new(limit = 10):
    print("****")
    print(submission.title)
    
    title = submission.title
    if "love" in title.lower():
        print(f"Found 'love' in {title}")

You can find the documentation for the praw package here.

For any future questions you might ask, please read the following to understand how to ask a good question. It will help you get better answers to your problems.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related