Loading video player…

Inspecting Response Headers

Download link from this lesson: https://api.worldbank.org/v2/en/indicator/NY.GDP.MKTP.CD?downloadformat=csv

00:00 The urlretrieve() function actually returns a tuple containing two items. The first is the local filename, and the second is an object containing response headers.

00:09 You can use this response header to get information regarding the file. Let’s try this. So first, you import urllib.request, and then you define the URL.

00:22 Instead of just calling the urlretrieve() function, we’ll unpack the response of the urlretrieve() function. So type filename, comma headers,

00:32 and now we call the urlretrieve() function by typing urllib.request.urlretrieve(), and then the first parameter to pass is the URL, and then we pass the filename as the string gdp_data.zip, and now the filename will contain the file path in the local machine, and the headers will contain metadata regarding the file. Let’s print headers to see what it contains.

01:00 So the headers contain a lot of information regarding the file like date, content type, content length, connection, and so on. The content type actually tells us if the file is a zip, an image, or a text.

01:13 The content length gives us the file size in bytes. Now if you want to check the size of the file, you can just get the content length using the header. You can do headers.get() and pass the key name that is "Content-Length",

01:31 and you’ll get the content length that is the size of the file.

01:36 Similarly, you can get things like content type, last modified, or any other header according to your requirement. urllib is great because it’s built in, but if you’re serious about downloading content from the web, then you’d commonly use a third-party package called requests instead of urllib.

01:55 requests is the industry standard for interacting with HTTP in Python.

02:00 In the next lesson, you’ll learn about requests and how you can download files using requests.

Become a Member to join the conversation.