How do you get the url from Submission object in PRAW?
Asked Answered
I

2

5

I'm using PRAW to create a Reddit bot that submits something once a day. After submitting I want to save the url of the submission and write it to a text file.

url = r.submit(subreddit, submission_title, text=submission_text)

The above returns a Submission object, but I want the actual url. Is there a way to get the url from a Submission object, or do I need to do something else to get the url?

Intercede answered 7/10, 2015 at 15:5 Comment(0)
H
6

submission.shortlink (previously .short_link) is what you're looking for, if submission.permalink wasn't good enough.

reddit = praw.Reddit("Amos")
submission = reddit.get_submission(submission_id="XYZ")
print submission.permalink
>>> www.reddit.com/r/subreddit/comments/XYZ
Hyperbolic answered 7/10, 2015 at 15:18 Comment(1)
.permalink is the URL you get when you click on a submission in the web interfaceRiviera
G
6

I see that @TankorSmash has answered your question already, though I thought I might add some fundamental knowledge for future references:

If you use "dir(object)," you'll be able to see both attributes and methods that pertain to the Reddit API (which you may use to test and see all properties that effect the given object being tested). You can ignore everything that starts with an underscore (most likely).

An example would be:

submissionURL = submission.url

Or you can go straight to source where PRAW is getting its data. The variable names are not set by PRAW, they come from this JSON (linked above).

Guest answered 11/11, 2015 at 5:51 Comment(1)
Hey, I know this is an old thread but I used this and for submissions with images I got an image link and for text based ones I got the comments link, could you please explain how this works.?Karaganda

© 2022 - 2024 — McMap. All rights reserved.