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

preg_match string from sentence if title has certain word

How to change a word on a line that has a certain string on it

How to check if a string does not contain a certain word?

How to check if post title already exists?

How to check if a list has a certain element in it

How to check if an R object has a certain attribute?

How to check if a user has a certain permission in a server?

How to check if dictionary has certain key (Python)

How to check if a certain word can be used for another word in Python

How to check if a word has a specific alphabet

How to check whether someone has a certain role in a certain server

Check exists post title

Check if WinList() contains a certain title

targeting last word of post title

(Notepad++) How to replace a text that has certain word in that line?

How can I check if the string contains certain word?

How to check if a certain df['column'] contains a word from a list Python?

How to check if a string contains a certain Chinese word in PHP?

How to check a text editor (file) for the presence of a certain word that the user entered

How to check if website has http/2 protocol support

How to check if a website has HTTP/2 protocol support

Bash: How can I check if a website has finished loading?

How to check if span has value even after the website refreshes

How to check if a string date has passed a certain date

How to check if a PNG has a certain RGB color using Python?

how to check if an html element has certain text with javascript

How can I check if a div has a certain class using puppeteer?

How do I check if a file has a certain mode in Rust?

How to check whether an object has certain method/property?