Hi, welcome to this section, we're going to work with rest APIs through all these videos.
Specifically, we're going to access various APIs from Python.
And then later on, we're going to write our own API also with Python.
So Python can both interact with APIs, but also create APIs.
Now what is an API?
This is what you learn in this video, and we're also going to look at an example of working with an
API from Python.
So specifically, we're going to work with the rest APIs, which is the most common type of API that
you will encounter.
So what is a race API?
The boring part first.
Well, API stands for representational state transfer application programming interface, but don't
get intimidated by the name because I'll explain you what API is.
We're using an example.
Everything will be clear.
No, the phone port to make you really understand what's a rest API is.
First, let's look at the normal web page, such as this one in here.
It's a space news website.
So there's a server somewhere that hosts a web app, and this web app serves this website.
These web pages to users like us.
So when we visit this web page, we get some results laid out in a friendly, easy to read format,
you know, nice font sizes and paragraphs.
Now this is easy to read by us humans, but not by computers.
So the language that has built such a readable format for humans is age HTML.
So the back of this web page stands HTML language.
Other browser renders that H.M..
No, the problem is, if you want to read this web page, it's OK to read the dots on the content information
with a computer program such as a Python script in order to get this data and do some other things with
those news or other data.
Then in that case, you could use a web scraping.
Both web scraping is often tough.
That's a lot of work involved because you have to pass all these information.
And here is where rest APIs join the game.
Rest APIs are web applications that don't serve web pages in a stable form, but they serve them in
other formats that are meant to be interpreted by applications easily.
So think of an API as a way for two applications to communicate.
So on one side, you have one application, for example, an application that has news, and on the
other hand, you have the application.
That's you, right?
With Python, in our case, that's Python application will access the data from.
The news application.
So let's look now at an API example.
The application here, which is on the server, we don't see the application itself, but what we see
is the dots of this application is giving to us.
So this data now is friendly for computers.
It's well-structured, it's in a Python dictionary like format, and it's called Jasons versus a JSON
format, and Jason is the usual formats that is served by APIs.
So for humans, we had HTML and for computers we have Jason.
So this particular site's called news API dot org lets us access.
These dots are in this format.
Now we're accessing these news from a browser, but we can do that from.
Python as well, so let's jump into Python, so I'm going to open a reptile to have an empty python
file.
And so through our process, we access this Web page using our browser in Python, we need to access
it's using requests, this library.
And then you create a request using requests that get.
And then here you enter, that's euro, that's you have entered in your browser.
You can get this euro from the election resources.
There's a there's a euro atocha there.
You can click that it will open in your browser, then copy it and put it inside this method.
And then the contents.
Will be our Jason.
And then if you print the content, you should get the same content that you got in here.
Well, this is in the form of a dictionary, it has keys and values.
So, for example, a status quo here has a value of OK, which is some information about the requests
that we made.
And then we have total results, so many news articles we have.
And then we have this articles key.
So now we can get to that article's key.
By using this syntax, since content is in the form of a dictionary, so print type.
Content.
Let me comment that out.
So it's a dictionary content, therefore we can use such syntax to get the value of the articles only.
So the article's key is connected to a list as valley and that lists as dictionaries also.
So it's nice that dots on boats computers can understand it's if you follow the correct syntax in your
programming language.
So if we follow this, we would say we want the item with index one, which is this dictionary.
So let's continue this sort of index zero.
It's the first item.
This warning here that we is perhaps the title.
And Ron.
And so we got the title of the first news article.
So that's how our program is communicating with with this Web app, with this API.
So the API app that is horses in this server is serving data to our application, to our python application
here.
So now we get those data and then we can do whatever we want with them.
In this case, we got the title.
You can also get the description so you can change that to this encryption.
Run.
And so you have some more information there, and you can explore more keys here to access those.
So the way our Python application is communicating with this API is through URLs through URL parameters
to be exact.
Because if you look closely to the URL in here, you have some parameters with their values, you URL
parameters.
For example, this is a sort of parameters as Q and title, it means what kind of news do you want?
In this case, we have stock markets as the keywords for the news we want to have.
So you can change that to something else, for example.
But you know it.
Percentage 20, which stands for a space and then you say states, so United's percentage 20 states
run.
And so you get the first news description for that query.
And then you have other providers such as from.
So this is the date.
Twenty twenty two to twenty seven, you can change that to 26 and you get that off for another period.
If there are news for that period and then you have soared by popularity language in English and each
parameter, as you see, it's divided by this and symbol and then you have the API key here.
The API key is something you need to get from news API.
That's all from that website.
Then you place that API key in here to make it possible to get data from this API.
So that was an example of working with APIs via Python.
Now if you're interested about news, this particular news API, how to get news from this API, I'm
going to elaborate on this script further in the next video.
So to improve it a little bit to make it more dynamic.
Thank you for following this, and I'll talk to you in the next video.