How to Fetch APIs with Python | API Fetching With Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back in this video i'll be going over how to fetch apis using python now i've made a similar video but that video was in react but in this video we'll be fetching using python and also a disclaimer if you're watching this video i assume that you already know how apis work so i won't do an explanation on how they work since i've already done that in a previous video if you'd like to know you can check that out now i don't want to waste your time so let's hop into it okay so first off if you've already seen the react video and understood how to fetch an api in react you should have no problem learning in okay so first off we need an api that we'll be fetching from and just like in the react video we will be fetching from the same api which is this random user.me api and we can go to this slash api route and it will generate random user data now i've opened up my editor and have it on the right with the json data on the left in my editor i've opened up a folder and created a python file called api.pi and this is the file we'll be writing our code in in the react tutorial we fetch the api and display the results on a web page but this time we're going to display the results in the terminal all right so first off we're going to need to install requests using pip if you don't know what request is request is the library that we're going to use to fetch the api so open up your terminal and then type in pip 3 install requests if you're on mac and type in pip install request if you're on windows so i'm going to hit enter and as you can see i already have it installed so it says requirement already satisfied so i'm just going to clear my terminal for now and once we've installed it we can import it into our python file so we can do import requests and this will import the request package into our app so next we're going to need a way to connect to this api so we're going to do response is equal to request dot get https colon slash random user dot me slash api and this will connect to the api and um we'll store our response in this response variable basically and next we're going to want to know whether our app has connected to it or not so we can just do print response dot status underscore code and what this will do is basically print out the status code and the status code will let us know whether our connection was successful or not if it was successful it will return 200 out of the status code so we can run this app using python 3 api dot pi if you're on mac and python app.pi if you're on windows so i'm just going to hit enter and as you can see it returned 200 meaning that our app is connected successfully to our api so since our app has successfully connected to this api we want to be able to print out the data that we receive from the api and we're going to print that out in json so we can just under this line we can type in print response dot json and this will return the json data that's that's returned on this page right here so we can run our app again so python3api.pi and then as you can see it returned the json data as well as the status code for the response so our api is working but this data that it's returning looks a bit messy so what we can do is print out specific parts of the data so for example if we want to print out their gender we will first need to access this results array and then since the gender is located in the first index we can access the first index and once we access the first index we can access the gender so in order to print out the gender i'm just going to create a gender variable and then have this equal to response.json and then we're going to get the results array and then we're going to go into the first index and then once we're in the first index we can just access gender and then next we can just print out gender so print gender and then save the changes and then rerun that app so python3 api.phi and as you can see at the bottom it has successfully returned the gender all right so now since we have their gender we can basically get anything else so for example let's say that we want to get their first name in order to get their first name we're just going to have to access this name right here and then get this first name right there so in order to do that i'm just going to create a variable called first underscore name response.json it's going to be equal to results and then the first index and then we're getting the name and then we're getting the first name and next all we have to do is just do print first underscore name and then if we rerun our app as you can see it printed out the gender and the first name now we want to get the last name we can do pretty much the same thing except we're just going to have to pass in last instead of first so i'm just going to copy these two lines paste them right below and then i'm just going to rename my variable to last name and print last name and then also at the end i'm just going to put last instead of first and then if we rerun our app as you can see it printed out the gender first name and last name now since our code is basically working um we won't be needing this since these two lines were just for testing purposes to make sure that our app was connecting to the api so i'm just going to remove these two lines and instead of printing out the first and last name separately we can print it out together so to do that i'm just going to remove this first name line and then what we can do is delete that print statement and just type in print then an f string first name and last name so last name right there then if we rerun our app it's just going to print out the gender and their full name right here and also instead of just printing out their name we can also print out their title so mr mrs and to do that what we can do is just create a title variable right here and then we're just go i'm just going to copy this line and then paste it there instead of getting first we're just going to get the title and then here print title dot and then first name last name so if we rerun our app as you can see it prints out the gender their title and then their first and last name right there now if we want to access the age what we can do is get this dob the date of birth and then access this age right here so what i'm going to do is create an age variable and then i'm just going to copy this for now so that i don't have to retype it so here and then instead of getting the name we're going to get this date of birth and then we're going to get this age which is right here so to get the age i'm just going to put data over there and then do print f string age page right there so if we rerun the app so python3api.pi as you can see it printed out the gender the title first name last name and their age alright guys that's it for this video i hope that this helped you better understand apis in python and i hope that this will become helpful in the future if you enjoyed make sure to leave a like and remember to subscribe since more videos will be coming soon and with that said see you in the next [Music] you
Info
Channel: Arpan Neupane
Views: 105,479
Rating: undefined out of 5
Keywords: flask, programming, python, coding, python3, flask authentication, coding with python, easy programming, python tutorial, python3 tutorial, how to learn python, coding videos, programming tutorials, python programming, api fetching using python, api fetching, what is api, how to use api, requests easy, api easy, requests tutorial python, api fetching with python, python api fetching, how to fetch api with python, fetching api, api fetches, what are rest api, what is rest api
Id: bKCORrHbutQ
Channel Id: undefined
Length: 7min 32sec (452 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.