Using Web APIs in Python
If this is the first time you are hearing that word, then it means "Application Programming Interface". It allows you to interface with someone else's data in a way which is predefined. This makes it much easier for their systems and your program to get the data you need.
Most services would require you to have an API key, which will most likely involve you signing up to their service (probably for free). This is so they can see who is accessing what data and possibly why. It's mainly like that so people cannot overwork the API service so others cannot get the data they need.
Now that we know what an API is, let's see what we are going to be using.
That's right, we will be using the Wikipedia API for a few reasons.
- The biggest reason being that we don't need an API key. This makes it so anyone can use their API, as some of you may not want to sign up or can't.
- It also has a large database of data to play with, which is always fun.
- Finally, it allows us to jump right into the code without wasting time with API key stuff.
Now that we have covered what we are using and why, let's jump into the code.
So, we are going to need a two modules to help us on our journey, both of which are in the standard library, just the way we like it.
- The first is
urllib.requestto help call the API which is web based. This allows us to request the data from the API.
- The last one is
jsonwhich will help us to read the data that we receive from the request to the API.
To import these, all we need to do is:
import urllib.request import json
We now need to request data from the API. You can view Wikipedia's API here, but we will be exploring what each thing that we used does, so don't worry to much.
Let's request the data from what the user wants to search!
# Let's get what the user want to search search = input("Search: ") # We will then add that to the url after the search param url = "https://en.wikipedia.org/w/api.php?action=opensearch&limit=1&format=json&search=" + search
Now we can explore the code a bit more. We first get the string that the user wants to search. Then we concatenate that to the url. Let's look a bit more at the url for a moment.
The url goes to what we call an endpoint, which in this case in
https://en.wikipedia.org/w/api.php. This is where API services can be queried. To make this easier (but also not so clear), we include the values of our search in the url. Those are the things after the
?, separated by
- The first one states the
action, in this case we a doing an
- The next one
limits the data to
- Next, we make sure that the return
- Finally, we
searchthe term we are looking for.
This should give you some idea of the structure of a query to an API. This will change depending on your API, so always have a look at the terms your API uses.
We can now open the url and get the data from it using
urllib.request like so:
# Let's then open the data we got (it acts like a file) and get the data req = urllib.request.urlopen(url).read().decode()
This gets the data which the url would return after being requested. We can then read its data which will come out as bytes. This is because Python doesn't encode data the same as the web does. So we have to decode it from utf-8 into what Python can understand, Unicode.
Now that we have the code, we can read the JSON which is received, read and decode. This is super simple:
# The we can load the json recived from the call data = json.loads(req) # Print the data to view it print(data)
This converts the JSON string into a Python Data Type for us to use in our code.
Now that you have the data to play with, you can do whatever you want with it. It also gives you some insight into how we can get data from APIs in Python. Many of the APIs you use will probably have a module to use in Python, Wikipedia has its own one, but knowing how to get the data yourself helps when there isn't a Module for it.
Have a great day!
This tutorial was suggested by @malvoliothegood .
If you have any suggestions for tutorials, leave them in the description and I'll be sure to have a look. If you like one in the description, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!
Looks great, very well written. I have never used an api or even though of using it but now i am going to challenge my self to make a weather application that grabs the current weather. Thanks!
Edit: You should also include how to use an api key because most places do require that.