How to Access Web APIs using Python Requests and JSON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay everybody welcome to python api requests using json json's case means javascript object notation quick shout out to my channel members thank you so much for supporting the channel directly click like subscribe and join if you can everyone so today what we're going to be taking a look at is using the request module and json module to do web api requests now an api is an application programming interface and the website that we're going to be in you know interacting with is called tvmaze.com and they have information about tv programs now real quick uh jason is something called i guess i mentioned earlier javascript object notation it is a way for information to be exchanged between programs it is text based and uh but it looks a lot well you'll see it later it is basically text-based but it has a very specific format that is easy to use especially in python let's go ahead and get started now i am assuming that you have already seen my requests demo if you don't know how the request module works go watch that first and then come back here so what i want to do is i want to connect to the tv maze api now there's a link here i'll put it down below it says tvmaze.com api brings you to this page and in this page it talks about the various api calls that you can do so it tells you here the root url is right here and here are some of the different types of api searches you can do and there's some other information here as well now for just an average person you don't really need to worry about you know all this crazy stuff over here let's just focus on the easy stuff over here now i've chosen to use the show single search as an example just because it's the simplest one that i could figure out so this is the url now notice it's not a complete url because you have to add the http api.tpmaze.com part to it but if i click this example i'm going to go ahead and open that in a new page and what it does is it returns json so i'm going to take a look at this is the raw data for that okay that's a little bit hard to see here but if i click here now this is something that i guess firefox does automatically so it gives you kind of an idea of what you're doing now json is a lot like a dictionary in python you've got a key and some sort of value so id is 139 url is this the name is girls this is the example they have and really good show i haven't seen it for a while but it's a it's a really good show if you get a chance check it out anyway so this is what the information i should say that we can get back from tvmaze.com about the show girls and now instead of doing this in the browser what we're going to do is we're going to do it over here in python which is pretty cool so let's go ahead and get started with that so i'm going to go ahead and use my import requests again this is from my other tutorial if you haven't seen that check it out i'm going to need the url and i'm just going to go ahead and basically just copy that from here so i'm going to go ahead and copy the example so i know the example works i'm going to cut off the question [Music] equals girl's part let me go ahead and make this a little bit wider so a little more to refer to here and the parameters the params you can call it whatever you want but i'm using params and so in the example that we we got earlier was girls okay so again i'm just copying what is over here and i put a capital g here i don't think that matters so what i want to do is i want to get a response from using the request module so i'm going to use requests.get and it's url params okay now this response is not text it is a response object again i explain that in my request demo check that out if you haven't and what we do is i'm going to say if response dot status code equals 200 200 means that everything went through perfectly there's no problems i'm going to go ahead and print the response dot text okay and that should be our json code that or i guess code uh response that we saw over here and this is a raw data okay it's not gonna be nicely formatted like this it's gonna be raw data so i should see that down below i'm going to do else you know print you know ffstring error you know and then i'm going to put the status code in here hopefully we won't have that but just in case response.status code in case i got the url mixed up or something like that so i'm going to go ahead and run this and see what happens you see there's a little bit of a delay and now you can see here is the json code for the json response so id 139 url blah blah blah blah which is pretty cool okay so very easy surprisingly easy actually if you think about it so we were able to just using a few lines of code i didn't even have to use this if stuff here but let's see one two three four basically five lines of code i was able to get that and print it out now the thing you have to remember is that this is actually text it's not a python dictionary looks like it but it's not a python dictionary it's text so we have to be able to pull that information out of there now the easiest way to do this that i know of is to convert this text into a python dictionary which is surprisingly straightforward to do that i'm going to go import json json and this is a python module that will let you manipulate json data so what i'm going to do here is i'm going to say the data equals jason dot uh what is it load s don't forget the s load is a different command see where it says loads str so it takes the string so response dot text and it will convert that into a data this data will become a dictionary it returns a dictionary so now i can go print data and see what we get here response is not defined because i spelled it wrong uh response rip stone response that's always embarrassing but you know it's good to see everybody does this so response and you can see it looks very much the same because the format's very very similar okay so i'm going to do is i'm going to go ahead and do import p print which is pretty print i'm going to go ahead and do print dot print data and run that again this gives us the data in a formatted kind of easy to read method so again you can see some of the keys that we can use so we got the name of the show is girls it premiered on a certain time had a rating and there's just different things that we can do with this so let's go ahead and pull out the name and let's say premiered and we'll do the summary so we've got three keys name premiered and summary so remember that so i'm not going to pretty print that anymore i'm just going to comment that out oops so what i want to do i said name equals premiered equals and what was it summary equals now remember this is a dictionary and if you look closely we got pretty lucky with this one and i chose it because this one's pretty simple these are the keys and they're like first level keys they're not like embedded down further now you see there are some dictionaries inside of dictionaries which makes a little bit more complicated but i'm sticking to the simple stuff so the name is going to give us this data the premiere is going to give us this data and the summary is going to give us this data so this is just like a normal python dictionary so data and this is going to be name i could use double quotes there it doesn't really matter data and premiere just make sure you spell that correctly and data summary okay and that's what i'll do is i'll go ahead and print that out so print uh f when i use f strings they're very convenient premiered on and print uh summary okay so name premiere on premiere okay and again this is a very very common pattern like encoding something like especially something like this first we pull out you know we get the we get the data somehow from somewhere in this case it's a response object from that response object we pull out the text that we need then we converted it to the data format that we needed and then we pulled out the individual pieces of data that we need so the name premiered and summary now again i didn't have to this didn't have to be name i could have called it show name or something like that this could have been np or s but again i like to use you know names that make a lot of sense so it's very easy to see what my program does so now if i run this you can see that i pulled out the information so the girls premiered on and here's the date and here is the summary now note this summary happens to have html tags in it so i assume this is i presume this was meant for the web but uh anyway that's that's a story for another day how to filter that stuff out um let's see what else do i want to do okay so i could change girls i'm going to try a couple different shows just to see what's going on here let's try star trek okay and you can see here uh star trek premiered on on june 24th 1964. that was a long time ago and you can see it talks about james kirk the enterprise etc etc and let's try next generation sorry i'm a big star trek fan sorry star wars people uh let's see if that pops up oh wow okay so star trek the next generation prepared premiered on uh was that nine whatever 9 28 september 28th 1987 that is also going back but not quite as far as the original and yeah so you can see that this tv maze website has quite a few different uh quite a lot of information i should say so yeah it's pretty cool and let's see what else let's try the mentalist so i'm a big mentalist fan to list as well let's go ahead and run that oh very cool okay so that is metals premiered on 2008 so 923 2008 very very interesting great show if you haven't seen it it's definitely on netflix so there you have it um it's surprisingly straightforward and easy to do this now again i you know i i prepared this ahead of time i've done this before so i already knew you know kind of how to pull the data out probably that's the hardest part about this is you know every website's going to return the data it's going to be json but what are the keys going to be how are they going to be nested and all that sort of thing so you have to kind of you know play around with pulling the data out but again for this one it's pretty straightforward it only returns one possibility and then again that is why i chose it now there are other you know open apis they're called and you can just access them without making any sort of you know account this is a nice one for getting started i would strongly recommend that you not put this in a i'm going to put it do you not put this in a loop because if you mess it up and you make too many requests you can get blocked what i might want to do with this program just to make it fun i might say show equals input you know please input a show name oops and then in here i would put you know show so that way i don't have to i'm not hard coding when i run the program it's going to ask me for a particular show and i'm going to say billions because i'm watching that right now and you can see here billions premiered on january 17 2016 is a complex drama it is absolutely a complex drama but it's really good check that out if you have a chance uh anyway so that is that that is how you basically pull data out of an api uh on a web page that provides it not all web pages provide this of course and yeah that's it you pull it out take the text convert it to a dictionary using the json module and then you pull the individual pieces of data out that you need just using standard diction you know python dictionary notation and then you output the data as it's needed i'll put all this stuff down below so you can copy it out and try it yourself so yeah good luck uh click like subscribe join whatever you can do and as i like to say keep on coding take care
Info
Channel: TokyoEdtech
Views: 44,984
Rating: undefined out of 5
Keywords:
Id: q30GEwUe5gY
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Mon May 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.