Open In App

Python PRAW – Getting the avatar icon of a redditor

Last Updated : 21 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
In Reddit, a redditor is the term given to a user. Each and every redditor has an icon on their profile which is their online avatar. We will be using the icon_img() attribute of the Redditor class to fetch the URL of the redditor's avatar. Example 1 : Consider the following redditor : The user name of the redditor is : spez. Python3 1==
# importing the module
import praw
import PIL
import urllib

# 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 name of the redditor
redditor_name = "spez"

# instantiating the Redditor class
redditor = reddit.redditor(redditor_name)

# fetching the URL of the image
url = redditor.icon_img

print("The URL of the icon image of " + redditor_name +
      " is : \n" + url)

# displaying the image
urllib.request.urlretrieve(url, "reddit.png")
img = PIL.Image.open("reddit.png")
img.show()
Output :
The URL of the icon image of prez is : 
https://www.redditstatic.com/avatars/avatar_default_19_A06A42.png
Example 2 : Consider the following redditor : The user name of the redditor is : AutoModerator Python3 1==
# importing the module
import praw
import PIL
import urllib

# 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 name of the redditor
redditor_name = "AutoModerator"

# instantiating the Redditor class
redditor = reddit.redditor(redditor_name)

# fetching the URL of the image
url = redditor.icon_img

print("The URL of the icon image of " + redditor_name +
      " is : \n" + url)

# displaying the image
urllib.request.urlretrieve(url, "reddit.png")
img = PIL.Image.open("reddit.png")
img.show()
Output :
The URL of the icon image of AutoModerator is : 
https://styles.redditmedia.com/t5_1yz875/styles/profileIcon_klqlly9fc4l41.png?width=256&height=256&crop=256:256, smart&s=94486fc13b9ca9e154e9e8926e3d8c43ccc80be3

Next Article
Article Tags :
Practice Tags :

Similar Reads