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


Talk Python's Mastodon Michael Kennedy's Mastodon