Commit 4d55a5dd authored by Benavides Ontiveros's avatar Benavides Ontiveros
Browse files

reddit data fetch with PRAW library test 1

parent 432e3cce
import praw
import json
# Reddit API credentials
reddit = praw.Reddit(
client_id="",
client_secret="",
user_agent="MyRedditApp/0.1 by DevTest0"
)
# Choose your subreddit
subreddit = reddit.subreddit("soccer")
# Get a subreddit object (e.g., "python")
subreddit = reddit.subreddit('python')
# Extract some useful data into a dictionary
subreddit_data = {
"id": subreddit.id,
"display_name": subreddit.display_name,
"title": subreddit.title,
"description": subreddit.public_description,
"subscribers": subreddit.subscribers,
"created_utc": subreddit.created_utc,
"over18": subreddit.over18,
"url": subreddit.url,
# Add more fields if you want
}
# Convert to JSON string
json_data = json.dumps(subreddit_data, indent=4)
print(json_data)
# Fetch top 5 hot posts
for post in subreddit.hot(limit=2):
print(f"\nPost Title: {post.title}")
print(f"URL: {post.url}")
print("Comments:")
# Load all comments
post.comments.replace_more(limit=0) # This removes "MoreComments" placeholders
for comment in post.comments[:5]: # Limit to top 5 comments
print(f"- {comment.body[:200]}") # Print first 100 characters of comment
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment