1

Trying to make a program that gets a random subreddit, displays the title to the terminal, asks the user if they want to see it and if so opens a browser with that subreddit.

I'm running into an issue here whilst doing so. I am very new with python/beautiful soup just started using it a few weeks ago for school any help will be very appreciated.

import requests
from bs4 import BeautifulSoup
import webbrowser

while True:
    url = requests.get("https://www.reddit.com/r/random")
    soup = BeautifulSoup(url.content, "html.parser")
    title = soup.find(class_="_2yYPPW47QxD4lFQTKpfpLQ").text ## this is supposed to get the title of the subreddit and this is where my error is occurring

    print(f"{title} \nSelect this subreddit? (Y/N)")
    ans = input("").lower()

    if ans == "y":
        url = "https://www.reddit.com/%s" % title ## Some issue, not sure what
        webbrowser.open(url)
        break
    elif ans == "n":
        print("Try again!")
        continue
    else:
        print("Wrong choice!")
        break

2 Answers 2

1

soup.find(class_="_2yYPPW47QxD4lFQTKpfpLQ") returns None if there is no element with the given class, That's why you got this error

Try to catch the error using try-except and print something if soup not found any title.

Try this code.

from bs4 import BeautifulSoup

import webbrowser

while True:
    url = requests.get("https://www.reddit.com/r/random")
    soup = BeautifulSoup(url.content, "html.parser")
    try:
        title = soup.find(class_="_2yYPPW47QxD4lFQTKpfpLQ").text
    ## this is supposed to get the title of the subreddit and this is where my error is occurring
    except AttributeError:
        print("Title Not Found")

    else:
        print(f"{title} \nSelect this subreddit? (Y/N)")
        ans = input("").lower()

        if ans == "y":
            url = "https://www.reddit.com/%s" % title ## Some issue, not sure what
            webbrowser.open(url)
            break
        elif ans == "n":
            print("Try again!")
            continue
        else:
            print("Wrong choice!")
            break
Sign up to request clarification or add additional context in comments.

2 Comments

This did work! But unfortunately that would render my program useless. Is there an alternative so I can ensure I am getting the title of the subreddit somehow?
I will edit the answer if I got some solution
1

Element you try to find() is not available in your soup and calling a methode on None raises this error, so:

  1. Always and at first check your response, does it include the expected information - You will be detected as bot, so add some headers:

    url = requests.get('https://www.reddit.com/r/random', headers = {'User-Agent': 'Mozilla/5.0'})
    
  2. Avoid to select your elements by dynamic looking classes, instead us static identifier and HTML structure, also select the right elements:

    soup.h2.get_text(strip=True)
    

Example

from bs4 import BeautifulSoup
import requests, webbrowser

while True:
    url = requests.get('https://www.reddit.com/r/random', headers = {'User-Agent': 'Mozilla/5.0'})
    soup = BeautifulSoup(url.content)
    title = soup.h2.get_text(strip=True)
    print(f"{title} \nSelect this subreddit? (Y/N)")
    ans = input("").lower()

    if ans == "y":
        url = f"https://www.reddit.com/{title}"
        webbrowser.open(url)
        break
    elif ans == "n":
        print("Try again!")
        continue
    else:
        print("Wrong choice!")
        break

3 Comments

Thank you for this information it is extremely helpful. Though I believe the issue of not getting a title still persists. the terminal says "title not defined" Any work around to get a title no matter what of the random sub reddit?
There is no such message in my example - What is your response or soup telling you if you print it? Is the element available?
I just realized my error, you changed code in the if statement too. it works perfectly now thank you SO much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.