Consuming HTTP Services in Python Transcripts
Chapter: Initial HTTP GET requests with the requests package
Lecture: A simple GET with requests

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Now that we're up and running with Requests,
0:05 let's make a simple get to a web page.
0:08 So first of all, we are going to need the Python script to run aren't we,
0:11 so let's go create a new Python file and we'll just call this simple get,
0:15 and PyCharm is asking should this go into the github repository, yes, it should.
0:21 Okay, so what we are going to do is we are going to start by importing Requests
0:25 and that is coming out of our virtual environment that we wired into PyCharm here,
0:30 and let's just make sure that works,
0:33 so we'll just do a little print here and we can make,
0:35 over here there is no our little run button, it's grey,
0:38 that's because there is no what is called a run configuration,
0:40 so let's create that by right clicking here, and saying run,
0:44 and notice it's using our virtual environment Python here, and it just says hello.
0:49 And so import, that worked well, our environment is working,
0:52 everything is looking great so far, so the first thing we are going to need is a url.
0:56 And let's just go to talkpython.fm and do a download there,
1:00 so what we are going to do is we are going to use requests
1:04 we are going to do a get, we are going to issue that get to a url,
1:07 we are going to capture that into a variable called response, or resp,
1:11 now we can come over here and just print out
1:13 and see what happens, see if this works,
1:16 so there is a little pause, it actually did the download and we got a 200,
1:19 that is awesome, so it looks like that worked.
1:22 HTTP 200, that is about as good as it gets.
1:25 So, let's actually add a little check to make sure this works,
1:29 so suppose I had something wrong here, we should have
1:31 a 404 or something to that effect, or even worse, something worse.
1:35 Okay, so it looks like the response came out great, so let's do a little test,
1:39 we'll say if resp.status_code ! = to 200, then something went bad,
1:45 we'll print error requesting url,
1:50 maybe we'll put the little response code here, something like that.
1:54 And then, I like to return or break, I can do an else here,
1:57 but let's go ahead and make this a little nicer, I am going to define a main method,
2:00 which is this, and that is going to be the code, and we'll just run this main method,
2:03 if and only if it's time, so here we can say return and we'll be out of here,
2:08 so we'll cancel a lot of this execution if there is an error but if there is not,
2:13 we are going to print out, let's just print out resp.text.
2:18 Now, that is going to show the entire page, you'll see it's screen by,
2:22 maybe we just want the first 500 characters, just to see the title,
2:25 so of course we can use a slice on the string, which is nice, here we go,
2:29 so let's see the title here, talk python to me podcast- alright, it looks like this works,
2:34 so super easy, request.get, give it the url,
2:38 check the status code and work with the text.