Skip to content
Related Articles

Related Articles

Improve Article

Python PRAW – Getting the subreddit ID that a comment belongs to in Reddit

  • Last Updated : 26 Jun, 2020

In Reddit, we can post a comment to any submission, we can also comment on a comment to create a thread of comments. Here we will see how to fetch the subreddit ID of the subreddit that a comment belongs to using PRAW. We will be using the subreddit_id attribute of the Comment class to fetch the subreddit ID of the subreddit that a comment belongs to.

Example 1 : Consider the following comment :

The ID of the comment is : fvib7aw




# importing the module
import praw
  
# initialize with appropriate values
client_id = ""
client_secret = ""
username = ""
password = ""
user_agent = ""
  
# creating an authorized reddit instance
reddit = praw.Reddit(client_id = client_id, 
                     client_secret = client_secret, 
                     username = username, 
                     password = password,
                     user_agent = user_agent) 
  
# the ID of the comment
comment_id = "fvib7aw"
  
# instantiating the Comment class
comment = reddit.comment(comment_id)
  
# fetching the subreddit_id attribute
subreddit_id = comment.subreddit_id
    
print("The ID of the subreddit is : " + subreddit_id)

Output :

The ID of the subreddit is : t5_2qh1o

Example 2 : Consider the following comment:



The ID of the comment is : fv9qvgo




# importing the module
import praw
  
# initialize with appropriate values
client_id = ""
client_secret = ""
username = ""
password = ""
user_agent = ""
  
# creating an authorized reddit instance
reddit = praw.Reddit(client_id = client_id, 
                     client_secret = client_secret, 
                     username = username, 
                     password = password,
                     user_agent = user_agent) 
  
# the ID of the comment
comment_id = "fv9qvgo"
  
# instantiating the Comment class
comment = reddit.comment(comment_id)
  
# fetching the subreddit_id attribute
subreddit_id = comment.subreddit_id
    
print("The ID of the subreddit is : " + subreddit_id)

Output :

The ID of the subreddit is : t5_2qizd

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :