Python Requests Beginner Tutorial - GET Requests With Translate API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everyone is anthe from pretty pretty here in today's video I'm going to talk about the request library in Python so this is a really cool library because it allows you to interact with the services outside of your own app so in this video I'm going to demonstrate a translation API so basically translating like English to some other language but there are so many other api's that you can use that can do various things so Google has a ton of api's you can go to the github API you can use the stripe API there are so many and by learning the basics of requests which you'll do in this video you'll be able to use any of those libraries so in this video I'm going to focus specifically on get requests so that's basically emulating the type of request that your browser performs so every time you go to a page on a website or type in something in the URL address bar your browser performs a get request so it's basically asking the server to give you some information like you're getting something from the server there are other types of requests where you can send some data to the server to change something on the server itself but those can be a little more complicated because it usually involves authentication so in this video I just want to focus on the gits requests which typically don't require that much authentication maybe just like an API key which the API in this video will use so let's get started the first thing that we need to do is install requests so to do that you can use pip so pip install requests you may want to create a virtual environment before doing this or you could use something like pip in which we'll create the virtual environment for you that's what I'm doing but once you install requests we can get started it's the only library that we need for this video so at the top of your file just create any file that you're going to execute I call my script PI and I'm going to import requests at the top so just like that so to begin with requests like I said in this video are just going to focus on get requests so basically emulating what a browser would do and to start I'm going to I get requests on my own website so this is what it looks like when I do it in the browser so it looks like a normal website but if I perform a request on it through the console let's see what happens or through this script so I'll use R as a variable to hold the response so just remember that r is for response maybe r es is better so response and then I'm going to use the request library that I import it and then use the function called git and then inside of the first argument and the only argument in this particular case I'm going to put the URL that I once again so HTTP slash slash pretty printed comm so this is my website so now I just want to print the response so whatever I get from here I just want to see it on the console so I'll run the script Python script PI and I see response 200 and that's it so let's begin with that what 200 means is it means everything was successful with the requests so 200 is a status code there are other status codes it's like 201 300 404 you've probably seen 404 before you've probably seen 500 before but normally the most common is going to be 200 that means you perform the requests and everything went well and the data was returned to you if you get a 404 for example so if I try a URL that I don't have so does not exists and I perform the request again I see responses 404 that's because I don't actually have a page that exists here now if I go here in the browser I do have a 404 page but the status code that this is returning is a fluorophore status code which means not found so let me go back to putting it to just the base URL and I see responses 200 if I want to get the status code more directly I can do re s so response dot status code and here it just returns 200 so if we try 4 does not exist again and I run it it returns 404 so that's very simple so what you would want to do in some time in some cases is check to see if the response was ok so you can do if response and if the response is a good status code so basically anything in the two hundreds are three hundreds then this will return true so print response okay and if it fails so if it has a status code in the for hundreds or five hundreds which means air then we'll print response filled so let's try this again we see response failed here because it's a 404 if I change it back to the base URL we get response okay so that's the status code like I said most of the time is going to be a 200 you will want to check for the status codes for the cases where things don't work as you anticipate but generally speaking the API is going to return the same response for you once you're done testing all the time really the only time that it doesn't is when something changes in the API itself but assuming that your clothes stays the same and the API is the same then you'll always get the same status until something changes like something fails somewhere or someone changes code you do want to have this check in your code just in case but just know that it's very useful for testing and then it's basically like a safety measure when your code is actually running somewhere so now I've covered the status code I'll briefly talk about the headers so normally and get requests you don't really care about the headers too much but the headers are something that are sent along with the requests so here so you send headers along with a request and you also receive headers worth the response so these are the response that and we see some information about the server so we see that as Engine X server it has the date here the content type which can't be important this is actually used internally by requests the Python request library so we'll talk about that momentarily but this is the content type the last modified you know when the page itself is last modified transfer encoding connection very encoding etag x frame options and content encoding so if you need any of this header information this is where you look in our particular case we really don't need any of the header information at most the only thing that we need is the content type because we need to know what format the data is in that were receiving and to actually see the data this is the interesting part this is where we can use Tex so there are a few ways that we can get the data but that text just basically returns everything in text format so it's really easy to see what it looks like so if I run the script again we now see HTML and if you look closely at the HTML this is the HTML that is used on my web page so basically what this is doing is it's returning all the HTML that builds this page so if I view the source here everything I see here and the view source is exactly what I get returned in the response here so if you wanted to do something with HTML pages like for an instance crawling the page or scraping the page what you can do is you can request the HTML and then you can use some other library let's say like beautifulsoup to actually go through the HTML and find particular things like if I want to find all the links in this file I could by using something like beautifulsoup which is another Python library so the last thing I'll do just to demonstrate is I'll save this to a file so let's see output equals open and we'll call this index.html and then I just want to write to it so output right response text and this can be done because the response is just HTML this doesn't work for everything but since I'm just using HTML this actually does work so right and then output close so close the file and now let me run the script again so let's see I generate an index.html file and now when I open it I see the structure of my page but it doesn't look exactly the same the reason why it doesn't look exactly the same is because this is not a web browser or web browser takes in all the components of the page so there are a couple of other requests that needs to be made for instance requesting the images and then requests seeing the CSS files so not everything gets returned here the images are ok but you see they're a little bit smaller than what you see in the page itself and actually is missing one of the images so it's because that images from an external source whereas these images are inline in the HTML so it just depends on how the page is built but as you can see it's similar but not exactly the same so with that we've actually covered really the basics of using requests so now let's get on to something a little more interesting using the translation API by Yandex to use the Yandex API first we have to sign up for Yandex so I already have an account and I'll post a link in the description below to Yandex so you can sign up once you're signed up and you're logged in you want to go down to technologies and then you want to look for translate API and then finally get a free API key here so I have an API key already but to create one you just click on create new key and then type in a description and that's it so after you have the key take all this as part of the key and paste it into your file so what I'll do is I'll just comment out all this I'll leave the import request at the top and now creates a variable called API key and all caps just as a way of telling myself that it's a constant so this is the API key from Yandex so the next thing I want to do is actually create a request to the Yandex API and to do that I'm going to look for the documentation so on the left hand side just click documentation and this will tell you what you need to do to perform a request so there are a few things on the left but basically the most important thing is translating the text so click on translate in the text and then they show you the documentation so with API documentation it can be a little difficult to read sometimes but in this particular case I think it's fairly straightforward so they have two types of responses that you can get you can get JSON responses or you can get XML responses generally speaking and especially in the Python request library it is much easier to work with Jason but if you like XML instead you can use that but for this video we'll be using JSON so to use Jason you're going to take the URL here so just ignore everything below the URL for now I'll just take the URL and put it into your file so I'll create another variable called a URL so this is the end point for the Yandex API so this is very similar to me requesting my website but instead I'm using the Yandex Translate API and you see Jason is in there which means it's going to return a JSON response so now what I want to do is basically start the first part of the request which is just putting the URL in there so I'll create the response variable and then requests that get and then I'll pass in that URL so it's always the first argument and then there's some extra information that I need to pass because I'm trying to translate something right so I want to translate a word from a source language to a target language so I know at least I need to specify a word to translate or a sentence and I need to specify the languages involved so I'll start with something very simple English to Spanish so if we looked if we look here at get the list of supported languages it's actually another API request but if you know language codes then it's very straightforward so I already know what languages are interested in but if you don't know try getting the list of supporting languages so the process is going to be similar to actually translating the text if you just read the documentation so there are three things that are required so the things that are required are the things that don't have the square brackets so a key text and link so the key is the API key the text is whatever I want to translate in the language is the language direction and if you look at the documentation below that part it tells you things in more details so basically the text is the text to translate that's very obvious the language of them is basically like a - from relationship or just to relationships so by using a - like E in - ru means you want to translate from English to Russian if you did it are you - Ian that means they're translating from Russian to English if you only want to specify the target language in this case are you it's going to try to figure out the language that its reading but for our case we're just going to focus on the - from and then here at the bottom we see there are some error codes so what we'll do is we'll try getting some of these codes so to pass in those arguments we're going to use URL parameters so if you're familiar with URL parameters is basically like the question work and then it's key value pairs so key one value one and then ampersand key - value - ampersand and then sometimes you have a list of things so you can have key - value 3 and so on but in our case it's going to be just three keys and in Python requests you could do that directly in the URL so you can ask a question mark here and then the key and the value but is better to use the params so I'm going to create a dictionary and then pass it to gets so this dictionary is going to have three things it's going to have the key the key is going to be the API key and if we go back up here we'll look at the other parameters we need text so we have the key text text equals and let's say hello for now and then the last thing is the language so language let's say English so Ian to Spanish yes and then to take this parameters dictionary and pass it we're going to use the same parameter name so params equals params and then we're going to print the text and let's see if we can get it working so I'll run the scripts alright so we see code is 200 we see the languages exactly what we specified so en tu ES and the text now is hola instead of hello that is because it translated hello to Spanish which is hola so if I use something like French which I believe is fr and we do that again we see Bonjour instead of hola and of course you can do this for any language I just note that with languages where the characters are different it may not display your console because console is generally like displaying only basic characters found in English so if I try Russian for example let's see if I can see it I can so Russian I see that I have no idea what it says if we try a language like Thai so th we see the question works the data is actually there is just the console itself can't display it so we'll go back to Spanish and we see that we can basically send requests and we get the response so if I use the status code again status code we'll see that the coat here is the same so 200 and if we look at the headers will see different headers from my website so we see the server is engine X but we see the content type has change content type as application JSON and then we see some other things so like I said the content type is usually the one that you're the most concerned about but in our case the library itself handles it because it allows us to use this response Jason so what this does it's a function or really it's a method on the response object and it converts the response to a JSON object and this allows you to access the data using dictionary keys or a list indices so if I say something like Jason equals response Jason and then I print Jason here's what happens so we see code 200 languages English - - yes and then text is hola so this looks exactly the same as the one before the formatting is a little different but this is just plain text this is an actual dictionary so if I want to access let's say the text here's what happens so it's a dictionary so I look for the text key running again and I see a list with B text so that's the format that they have is just a list but I see hola there so because it's a list I can look for the first item in their lists and I get hola so now if I wanted to change the text to by run it I see by is the same in Spanish I guess I don't know how to say by so let's try goodbye adios okay so I know that one adios in Spanish means goodbye in English so by using the JSON objects so converting its adjacent first so I have to go through this step and then accessing the key text and then the first item in the list which is the only item in the list if I remove this maybe there are more items in the list just adios I can get the data that way so now let's try changing this so we can get a different code so 200 is what we saw before let's try invalid API key so a 401 error so to do that I just have to mess up the API key a little bit and what I'm going to do is print the response status code this time if I run it I see 403 so 403 is the status code and 403 mmm they actually don't have it in the API documentation which is kind of weird let's try breaking it on this side because I'm imagining that first part the the first part of the API key is going to be the same for all keys but yeah it still doesn't change it so they need to update their documentation but just know it's a 400 error so it's an error it's not something like a 200 which is good blocked API key I don't know how to trigger that maybe if I perform like a million requests very quickly they will block me same thing with 404 so basically it's exceeded the daily limit on the amount of translate X I can probably make that happen by just running this over and over again but I'm not going to do that I exceeded maximum text size let's see if there's something it's 10,000 characters I don't want to build something that's 10,000 characters but just know that there is a maximum and then the text cannot be translated so I think I can do that so if I just put in some random characters like that let's see what happens I get a 200 so now I'm curious with the responses so response text so it just gives me that response maybe if I put numbers can't translate numbers it does so it translates the numbers to Spanish so me Leona's and the rest so I guess that works I don't know how to put in some text that can't be translated maybe some random characters let's try that no that's fine so I don't know what would trigger that particular error but just know that a 501 error does exist so this API is very simple and most API is where you just get data from a service are going to be very simple so in the next video I'll talk about you know how to perform a request to API is that allow you to send information to them so it updates something on their server so for example let's say you're using the stripe API you can send a payment to stripe and basically stripe will then take that payment information and charge someone's credit card and deposit money into your account so that's much more complicated than just getting data and nothing actually changes in the case with stripe you're modifying something on their server and actually in to bank accounts and the bank account of your customer and in your bank account because you end up getting you the money so I'll talk more about post requests and the other requests where you can actually change things but get requests are probably the most common and a lot of times isn't the most fun because you can access so much information if you just find the right API so translating is a pretty interesting thing but there are tons of other things that you can do so that's pretty much it for this video if you have any questions about using the request library you can ask me a question in the comments below if you're interested in a course on requests I have a whole course called learn Python requests just go to my website then courses and then learn Python requests and you can learn the things that kind of go into requests so it's more comprehensive than this video it talks about you know testing the API using tokens it has three different example api's getting files images time outs exceptions and so on so if you're interested in that you can check out my website so that's it for this video if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe so thank you for watching and I will talk to you next time
Info
Channel: Pretty Printed
Views: 22,792
Rating: 4.9802957 out of 5
Keywords: python requests, requests, requests tutorial, python requests tutorial, beginners, tutorial, translate api, language translation api, yandex translate api, api
Id: BI4HxHn1HmQ
Channel Id: undefined
Length: 24min 29sec (1469 seconds)
Published: Mon Apr 15 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.