-
Notifications
You must be signed in to change notification settings - Fork 0
Getting Started
- Obtain a Reddit Instance
You need an instance of the Reddit class to do anything with PRAW. There are two distinct states a Reddit instance can be in: read-only, and authorized.
If you want to do more than retrieve public information from Reddit, then you need an authorized Reddit instance.
import praw
reddit = praw.Reddit(client_id='my client id',
client_secret='my client secret',
user_agent='my user agent')
- Obtain a Subreddit instance
To obtain a Subreddit instance, pass the subreddit’s name when calling subreddit on your Reddit instance.
# assume you have a Reddit instance bound to variable `reddit`
subreddit = reddit.subreddit('redditdev')
print(subreddit.display_name) # Output: redditdev
print(subreddit.title) # Output: reddit Development
print(subreddit.description) # Output: A subreddit for discussion of ...
- Obtain Submission Instances from a Subreddit Now that you have a Subreddit instance, you can iterate through some of its submissions, each bound to an instance of Submission. There are several sorts that you can iterate through:
- controversial
- gilded
- hot
- new
- rising
- top
Each of these methods will immediately return a ListingGenerator, which is to be iterated through. For example, to iterate through the first 10 submissions based on the hot sort for a given subreddit try:
# assume you have a Subreddit instance bound to variable `subreddit`
for submission in subreddit.hot(limit=10):
print(submission.title) # Output: the submission's title
print(submission.score) # Output: the submission's score
print(submission.id) # Output: the submission's ID
print(submission.url) # Output: the URL the submission points to
# or the submission's URL if it's a self post
Attributes that typically belong to objects of this class
- Convert pandas and save .csv
import pandas as pd
subreddit2 = reddit.subreddit('rstats')
r_subreddit = subreddit2.new(limit=10)
for i in r_subreddit:
reddit_data.append(
[i.title]
)
df = pd.DataFrame(reddit_data)
df.to_csv('FILENAME.csv', index=False)
Above only the title of the reddit posts is saved, but any number of attributes can be stored. To see what attributes are available you can built-in vars() function.
subreddit = reddit.subreddit('rstats')
print(subreddit.title)
pprint.pprint(vars(subreddit))
To obtain the attributes of Submission you can run the following code. I have given a list at then end. Also read Doccumentation
# assume you have a Reddit instance bound to variable `reddit`
# obtain an id for a submission (a single post) using the previos methods
submission = reddit.submission(id='ga9uns')
print(submission.title) # to make it non-lazy
att = (vars(submission))
pprint.pprint(att.keys())
If you have a PRAW object, e.g., Comment, Message, Redditor, or Submission, and you want to see what attributes are available along with their values, use the built-in vars() function of python. For example:
import pprint
# assume you have a Reddit instance bound to variable `reddit`
submission = reddit.submission(id='39zje0')
print(submission.title) # to make it non-lazy
pprint.pprint(vars(submission))
PRAW uses lazy objects so that network requests to Reddit’s API are only issued when information is needed. Here, before the print line, submission points to a lazy Submission object. When we try to print its title, additional information is needed, thus a network request is made, and the instances ceases to be lazy. Outputting all the attributes of a lazy object will result in fewer attributes than expected.
['_reddit',
'_fetched',
'display_name',
'_path',
'user_flair_background_color',
'submit_text_html',
'restrict_posting',
'user_is_banned',
'free_form_reports',
'wiki_enabled',
'user_is_muted',
'user_can_flair_in_sr',
'header_img',
'title',
'icon_size',
'primary_color',
'active_user_count',
'icon_img',
'display_name_prefixed',
'accounts_active',
'public_traffic',
'subscribers',
'user_flair_richtext',
'videostream_links_count',
'name',
'quarantine',
'hide_ads',
'emojis_enabled',
'advertiser_category',
'public_description',
'comment_score_hide_mins',
'user_has_favorited',
'user_flair_template_id',
'community_icon',
'banner_background_image',
'original_content_tag_enabled',
'submit_text',
'description_html',
'spoilers_enabled',
'header_title',
'header_size',
'user_flair_position',
'all_original_content',
'has_menu_widget',
'is_enrolled_in_new_modmail',
'key_color',
'can_assign_user_flair',
'created',
'wls',
'show_media_preview',
'submission_type',
'user_is_subscriber',
'disable_contributor_requests',
'allow_videogifs',
'user_flair_type',
'allow_polls',
'collapse_deleted_comments',
'emojis_custom_size',
'public_description_html',
'allow_videos',
'is_crosspostable_subreddit',
'notification_level',
'can_assign_link_flair',
'accounts_active_is_fuzzed',
'submit_text_label',
'link_flair_position',
'user_sr_flair_enabled',
'user_flair_enabled_in_sr',
'allow_chat_post_creation',
'allow_discovery',
'user_sr_theme_enabled',
'link_flair_enabled',
'subreddit_type',
'suggested_comment_sort',
'banner_img',
'user_flair_text',
'banner_background_color',
'show_media',
'id',
'user_is_moderator',
'over18',
'description',
'is_chat_post_feature_enabled',
'submit_link_label',
'user_flair_text_color',
'restrict_commenting',
'user_flair_css_class',
'allow_images',
'lang',
'whitelist_status',
'url',
'created_utc',
'banner_size',
'mobile_banner_image',
'user_is_contributor']
_comments
_comments_by_id
_fetched
_reddit
all_awardings
allow_live_comments
approved_at_utc
approved_by
archived
author
author_flair_background_color
author_flair_css_class
author_flair_richtext
author_flair_template_id
author_flair_text
author_flair_text_color
author_flair_type
author_fullname
author_patreon_flair
author_premium
awarders
banned_at_utc
banned_by
can_gild
can_mod_post
category
clicked
comment_limit
comment_sort
content_categories
contest_mode
created
created_utc
discussion_type
distinguished
domain
downs
edited
gilded
gildings
hidden
hide_score
id
is_crosspostable
is_meta
is_original_content
is_reddit_media_domain
is_robot_indexable
is_self
is_video
likes
link_flair_background_color
link_flair_css_class
link_flair_richtext
link_flair_text
link_flair_text_color
link_flair_type
locked
media
media_embed
media_only
mod_note
mod_reason_by
mod_reason_title
mod_reports
name
no_follow
num_comments
num_crossposts
num_duplicates
num_reports
over_18
parent_whitelist_status
permalink
pinned
pwls
quarantine
removal_reason
removed_by
removed_by_category
report_reasons
saved
score
secure_media
secure_media_embed
selftext
selftext_html
send_replies
spoiler
stickied
subreddit
subreddit_id
subreddit_name_prefixed
subreddit_subscribers
subreddit_type
suggested_sort
thumbnail
thumbnail_height
thumbnail_width
title
total_awards_received
treatment_tags
ups
upvote_ratio
url
user_reports
view_count
visited
whitelist_status
wls