 Hey, what's going on everybody? My name is John Hammond and welcome back to another Python programming tutorial We're still looking at the url lib module and this time we're moving on to the url retrieve function So this one's kind of cool because it allows us to download a file off the internet It copies a network object denoted by url to a local file on our computer It takes obviously the url of the object or the data that we want and the optional parameters are like a file name To save it as a report hook function to deal with it and handle it as a network connection has been made And we're retrieving blocks and data and it dated to pass to the actual address on the internet as like a post method Or anything that we actually need to give it so The description in the documentation and gives us this same kind of spiel The second argument will specify the file location to copy it to if it's not present The location will be a temporary file that's a with a generated name The third argument being the report hook is a hook function. It will establish network connection blah blah blah It takes three arguments, but I'm probably actually not even going to cover this or show it as an example because I honestly never really use it and data again is the Optional post information we can give to the web page that has to be encoded like you saw in url open so The way this function works though It'll return a tuple and the file name is going to be where the file is actually saved Local file where the object can be found and headers actually the information that we Receive back to us in the info object That's actually returned by the url open function that we just saw in the last tutorial in the last video Okay, something else to note is that url retrieve the function will actually raise an error the content to short error When it sees that the amount of data that was retrieved or was given to us is actually less than what we thought it would be and We test that by the content length header of The data and think that we're actually downloading So this might happen if the data is the download is interrupted or we don't actually finish downloading It's interesting though because it's treated like a minimum value as a lower bound If there's more to read then the function will retrieve more all of the data But if there's less data available, it'll freak out You can still actually see the downloaded data if for whatever reason the download is stopped But it's stored in the content attribute of the exception and the error that's raised So if the content length header isn't actually available if it's not supplied from the network address It doesn't have anything to compare it to nothing to reference or uses a benchmark So the function actually can't check The size of the day that it should expect it'll just give you the file in that case You just kind of have to assume or hope that the download was successful So all this stuff is something to keep in mind as you program if you ever write a script that automates some download or something It might be good to keep track of this exception or display to the user or let them know that this could happen or You of course as a programmer would kind of set up Handling if you see this error and just leave a message to the user and probably give them What you actually retrieve in the content attribute of the error? So All right, let's actually do this thing. Let's actually take a look at this function and use it now I'm gonna go to Google and just find some image to download all let's say Python Yeah, you saw some Pokemon Vietnamese crystal in there. You saw that you saw that Google search. That's cool Don't worry Let's just download this. I'll fire it up into idle URL lib URL lib dot URL open. Oh, sorry retrieve is a new one that we're looking at It takes the URL and I'm not gonna save this as a variable. Actually. Yeah, let's save it as an object. Let's just say download downloaded downloaded equals this thing and What's actually in that is hey this temporary file? Let's go check that out. That's got to be our file Open up file handler file manager and Hey in my temporary directory, we've got this image and that's exactly what I found on Google and wanted to download so nifty Also, we got here. We can of course specify Again a file name to save it to you. Let's just say mine Png and now in the tuple. It's actually in that directory Actually my home directory. That's where my idle shell is by default My local directory where I'm currently working. So let's go check that out mine.jpeg and There it is sweet All right. What else we got here? Again, I'm probably I'm not gonna work with the report hook and I don't have any data to show off to you guys So again, that's just a simple simple demonstration of just downloading something with URL retrieve The URL lib module it saves it as a temporary file by default But you can supply location for it and you can actually work with the headers Like there's the instance and we can actually take a look at what we saw With items and stuff like that to take a look at what it is that we're seeing there. All right, cool Thanks for watching guys. Hope you enjoyed this tutorial. That's all I really wanted to show off in this one And I'll see you again