Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Python for Humans
Lecture: Requests: HTTP for Humans

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The first of the two packages that we are going to look at is the Requests package. It turns out the Requests is the most popular package for Python,
0:11 let's look at its website really quickly. So over here you can see HTTP for Humans, very nice, "the only Non-GMO HTTP library for Python,
0:20 safe for human consumption", basically the idea of Requests is the urllib, urllib2
0:25 that are in Python, are cumbersome and hard to work with and overly complicated. Here is a re-imagine of it by a guy named Kenneth Reitz
0:32 who does amazing work to make it much more delightful and easy to work with.
0:37 You'll see this package gets a little bit of traffic, so if I search for downloads, Requests is one of the most downloaded Python packages
0:43 of all time pulling in over 7 million downloads every month. Think about that, 7 million downloads a month. And it's been around for really long time.
0:53 There is actually some talk about making Requests the new urllib more or less like bringing this into the standard library to keep Requests more agile,
1:02 they decided to keep it out, nonetheless, Requests is even recommended over the built in URL libraries: urllib, urllib2 and so on.
1:09 So let's do something awesome with Requests, so over here we have a URL to the omdbapi, that's the Open Movie Database API,
1:17 and we can do search here and get the data back as JSON and do a search for some text so we are going to ask the user
1:24 to enter some kind of text like "enter the name of a movie" or whatever and it will go pull that down.
1:29 We did play with this in another example previously but we didn't focus on Requests.
1:33 So in order to do this, the first thing we have to do is get started. We've already installed Requests, and of course,
1:40 we talked about pip and packaging and pip installing various packages but also from PyCharm we can come over here and look at our current environment
1:49 and see that we already have Requests installed but in fact it's a little bit out of date we won't mess with it, but we could upgrade it.
1:56 If we didn't have this installed we could hit "+" and say "hm,
1:59 I am looking for Requests", it turns out there is a lot of stuff that's built upon Requests here,
2:06 did you know there is requests-middleware, requests-guards, requests-ftp, requests-cloudkit? All of these things, but here is the one that we want
2:13 and we can just hit "boom, install" but great, it's already installed.
2:17 So let's just use it, remember "import antigravity"? Well, here is "import requests".
2:22 So the way it works is we get a response back and we give it some URL here, and URL we are going to construct from the user text,
2:29 we probably should check "if response.status_code" is not equal to 100, print, "wow, that is code such and such".
2:39 All right, so hopefully we don't end up in that case, but you never know. Now let's go down here and actually get the data,
2:45 so we made this request and everything was OK, we'll say that the data is going to be response.json,
2:52 so that's going to actually finish downloading all the text and the response,
2:55 convert it from json into a dictionary so this is going to be like a movie lookup.
3:00 Now, the format of this json is a little funky, if we actually want to search data, we have to go and ask for the search text here
3:07 and then we can say "for m in search" and we could print out just the title of the movie
3:12 we are looking for, and that comes in as title, something like that, and great, that's it.
3:18 Access is API, download it, possibly even authenticate against it, all we'd have to do is put the user name and password in here and we are done.
3:25 So this is just part of the power being leverage all these amazing packages on PyPi, let's see if it works, that would be a good thing.
3:32 So I want to search for let's say "Night Rider" can we find it? I don't see Night Rider but there sure is a bunch of stuff about nights,
3:39 let's try one more, see if Silicon Valley is there, Silicon Valley fantastic,
3:45 the Spirit of Silicon Valley, the Hermits of Silicon Valley, all those things, and we didn't get just the title back, we got lots of data, like that,
3:54 we just happened to only be printing out the title. So here is one piece, one example of looking just beyond the standard library
4:01 out in the broader ecosystem where you'll find amazingly powerful packages to help you,
4:06 here it is in a graphic, we'll say "import request", generate the URL, "requests.get" given the URL, check the status code, otherwise,
4:15 we'll just call .json, pull out the data boom, couldn't be easier.

Talk Python's Mastodon Michael Kennedy's Mastodon