In this video, you learn how to scrape the comments off of Reddit posts, Reddit submission via Python.
So what is Reddit?
Reddit is this website and it consists of subreddits, so subreddits are like forums and each forum
has a particular interest.
For example, this one here is Reddit dot com slash or slash cats.
This is a cat's subreddits where people talk about cats and postcards, videos and so on, and you have
all sort of subreddits for every possible thing you can imagine on Reddit, so everyone can post.
Everyone can comment, and everyone can use Python to do those things.
So to post and comment and scrape comments from these posts and so on.
So in this video, we're going to scrape the comments off this post here.
For example, you can pick any posts that you like, so just go to Reddit.
So that's the home page, for example.
And you see different ones if you click on one that should take you to that post.
So if I click.
On this.
Cat posts, it should take me to that post, so to access read it via Python, you need to create an
app to do that.
You should go to your profile here selected and then go to user settings, then go to safety and privacy
and scroll down.
And you should see here manage third party app authorization.
If you don't see vets, you can also go directly to this page by entering Grady dot com slash prefs
slash apps.
But you should create a Reddit account first, and then you should go to this page.
So if you scroll down here, you see these bots and create another app.
So I'm going to click on vets, give a name to the app, automates with Python two, for example, and
click on scripts.
The description is optional.
So is the airboats URL.
For Redirects your URL.
Just write something like HTP s colon slash slash example.
Dot com and press on Create app.
So that is the app I just created, and now I have this idea of the app, the secret of the app, and
we should use them in Python.
So let's go back to our Python script and import profile.
So Pro is a Python third party library used to allow us accessing the Reddit's websites.
So first of all, we should create a accredits instance using Proudlock to read it.
So that is a class now.
The first arguments you need to give is the user agent.
Now this is some information that your script will send to the Reddit server, and this is not really
important, so you can just write something like true.
What is important is the client I.D., which consists of string, and you can get it.
In your it's Abovitz, you created so that one on the personal use script that is the text you wanted,
copied and pasted in here, then you need to give a client secret.
Now this is getting long along the line, so I'm going to enter here.
Two splits in the next line.
The client's secret is again, the string this one.
Copy that, place it there, your red, it's username, in my case, this is this one in here automates
with Python.
And it's also reflected in here, so automate with Python and the password of you ready to count.
So this is just a throwaway account so that I created for this app.
So these dots are will not work if you try them because I'm going to delete the app and also change
the passwords.
So please create your own account.
So username and password is there.
And therefore we do have this accredits instance now.
So that is how you create a Reddit instance.
I just shifted this a bit to the right to make them more visually appealing.
So what is our purpose in this video?
Our purpose is to get the comments off a post, so we need to give our Python program the URL of a post
as a string.
So that post was.
Just pick whatever post the likes of this one.
And you want to be inside that post.
So if you see this view with comments on the posts, that means you are in that post page or submission.
It's also called a submission.
So you want to copy that you were l pasted there and then create a post variable Reddit submission.
And this gets a useful argument which a sequel to the you variable.
So that's one thing they know that we have.
I paused.
If you want to check what we have so far, you can print it out and see if things are working.
So we don't get an error.
However, we don't see any contents, either.
That's just the idea of the post.
And if you want to get the text the body of the post, you want to a polite self text.
And run.
Now, be aware of that for ports that don't have techs.
You're going to get that empty streak.
So for example, this only has a photo, a picture.
So if you find a post with texts such as.
This one in here.
So that's the title.
And that is the text of the post.
If I copy that, you know and place it's in the you are a variable there.
If I were doing this now, I'm going to get the body of the post printed out so they'd close off our
everything.
This one in here, if you want to get the title, you want to apply.
That's title run.
And that is the title, and that is the text of the post to get the comments.
What he wants to do is he wants to iterate for comments in.
Post those comments.
And you want to print perhaps comment that body, so a comment.
This is a list of comments and a comment as an object which has a body attributes.
So if you wrong that.
You should get all the comments.
That's users who left.
So if you want to see, for example, the length of the comments post that comments, you should apply
that.
And because there are too many comments here in the terminal, we don't see the length, so I'm going
to comment that.
Ron, again, so I see no.
Down here.
Yeah.
So it's a hundred ninety three comments.
But if you look at here, then you see that there are four hundred fifty three coalmines.
But that is total comments which are nested comments as well, such as this one in here.
But top level comments are these ones.
So that's when it's a top level of comments and then goes on here.
This one is a top level comment and so on.
So there are 193 such top level comments, so that should give you the top level comments.
So that is how you can access Reddit and scrape information.
For more details, you should look up at the Pro Library documentation.
It has more things you can do, so that's for now.
In the next video, we're going to get the posts that were created during the last 24 hours on a particular
subreddit.
So let's do that in the next video.