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