How To Use Reddit Api To Pull Data
Reddit API – Overview
In an before post "How to access various Web Services in Python", we described
how we can admission services such as YouTube, Vimeo and Twitter via their API'due south.
Annotation, there are a few Reddit Wrappers that y'all can utilise to interact with Reddit.
A wrapper is an API client, that are commonly used to wrap the API into like shooting fish in a barrel to
utilize functions past doing the API calls itself.
That results in that the user of it can be less concerned with how the lawmaking
really works.
If you don't use a wrapper, you will have to access the Reddits API direct,
which is exactly what we volition do in this post.
Getting started
Since we are going to focus on the API from Reddit, permit's head over to their API
documentation. I recommend that you get familiar with the documentation and too
pay extra attending to the the overview and the sections virtually "modhashes",
"fullnames" and "type prefixes".
The upshot from the API will return every bit either XML or JSON. In this post we will
use the JSON format.
Please refer to in a higher place mail or the official documentation for more information
about the JSON structure.
API documentation
In the API documentation, you can see at that place are tons of things to practise.
In this postal service, we have called to extract information from our own Reddit account.
The data we need for that is: GET /user/username/where[ .json | .xml ]
GET /user/username/where[ .json | .xml ]
? /user/username/overview
? /user/username/submitted
? /user/username/comments
? /user/username/liked
? /user/username/disliked
? /user/username/subconscious
? /user/username/saved
Viewing the JSON output
If nosotros for example desire to use "comments", the URL would be:
http://www.reddit.com/user/spilcm/comments/.json
Yous tin can meet that we have replaced "username" and "where" with our own input.
To see the data response, you can either make a curl request, like this:
curl http://www.reddit.com/user/spilcm/comments/.json
…or just paste the URL into your browser.
Y'all can see that the response is JSON. This may be difficult to look at in the
browser, unless y'all have the JSONView plugin installed.
These extensions are available for Firefox and Chrome.
Offset coding
Now that we have the URL, let's start to do some coding.
Open upward your favourite IDLE / Editor and import the modules that we will need.
Importing the modules. The pprint and json modules are optional.
from pprint import pprint import requests import json
Make The API Call
At present its time to make the API call to Reddit.
r = requests.become(r'http://www.reddit.com/user/spilcm/comments/.json')
Now, nosotros have a Response object called "r". We tin can go all the data nosotros demand
from this object.
JSON Response Content
The Requests module comes with a builtin JSON decoder, which we tin use for with
the JSON information.
As you could see on the image to a higher place, the output that we get is not really what we
want to display.
The question is, how practise we extract useful data from it?
If we just desire to look at the keys in the "r" object:
r = requests.get(r'http://www.reddit.com/user/spilcm/comments/.json') data = r.json() print data.keys()
That should give usa the following output:
[u'kind', u'data']
These keys are very important to us.
Now its time to become the information that nosotros are interested in.
Get the JSON feed and copy/paste the output into a JSON editor to go an easier
overview over the data.
An easy manner of doing that is to paste JSON upshot into an online JSON editor.
I use http://jsoneditoronline.org/ but any JSON editor should do the work.
Let's see an example of this:
r = requests.get(r'http://www.reddit.com/user/spilcm/comments/.json') r.text
As you tin can encounter from the image, nosotros get the same keys (kind, data) as we did before
when we printed the keys.
Convert JSON into a lexicon
Let'south catechumen the JSON data into Python dictionary.
Yous tin can do that similar this:
r.json() #OR json.loads(r.text)
Now when we accept a Python dictionary, nosotros offset using information technology to go the the results
nosotros desire.
Navigate to observe useful data
Just navigate your mode down until you lot detect what you're after.
r = requests.become(r'http://www.reddit.com/user/spilcm/comments/.json') r.text information = r.json() print data['data']['children'][0]
The result is stored in the variable "data".
To admission our JSON information, nosotros simple utilise the bracket notation, like this:
data['central'].
Remember that an assortment is indexed from zip.
Instead of press each and every entry, we tin utilize a for loop to iterate
through our dictionary.
for child in data['data']['children']: impress kid['data']['id'], " ", kid['information']['author'],child['data']['torso'] impress
We can access anything we want like this, only wait up what information yous are
interested in.
The complete script
As you lot can run across in our complete script, we only have to import ane module:
(requests)
import requests r = requests.get(r'http://www.reddit.com/user/spilcm/comments/.json') r.text data = r.json() for child in data['information']['children']: print child['information']['id'], " ", child['data']['author'],child['data']['body'] print
When you run the script, you should meet something similar to this:
More than Reading
http://docs.python-requests.org/en/latest/
Recommended Python Grooming
Grade: Python 3 For Beginners
Over fifteen hours of video content with guided educational activity for beginners. Acquire how to create real world applications and master the basics.
How To Use Reddit Api To Pull Data,
Source: https://www.pythonforbeginners.com/api/how-to-use-reddit-api-in-python
Posted by: mcconnellthentell.blogspot.com
0 Response to "How To Use Reddit Api To Pull Data"
Post a Comment