#100DaysOfCode in Python Transcripts
Chapter: Days 43-45: Consuming HTTP services
Lecture: Demo: Downloading search results

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we have the basic structure of our application written here, but now we want to actually go, when somebody calls this function,
0:08 we want to go and download this result from our search service, so, recall over here in our movie search service, we put something like this.
0:18 We say api/search/{keyword}, and then the search will happen. So let's go over here and first create the URL. And the URL we're going to use f-strings.
0:29 This is a Python 3.6 feature, if you don't have Python 3.6 or above, then you're going to need to do the older style format.
0:37 So we'll say like this, and we want to replace that little part with the variable keyword, so in Python 3.6 with these f-strings,
0:45 you can say, {keyword}. Notice the autocomplete. So, let's begin just by printing out URL, and just make sure that we're on the right path.
0:55 So we're going to come over here, and we're going to call result... Spelling is hard. Results equalsapi., notice there's our little thing,
1:04 and let's just, for now, say this is going to be runner. Going to search for runner. So if we run this,
1:11 that looks pretty good, we can click it and test. Okay, looks like we got the maze runner and runner runner, that's a lot of running.
1:18 And so this is working. Now instead of printing out this, let's actually use it. So we'll say response equals requests, which we've already installed,
1:28 get, and we'll pass the URL. We could go ahead and just work with this. We could say response.text, unless that failed, unless the, say,
1:37 wireless is down or something. So we need to be careful and say, I want to make sure there was no error. There's a status code, you could check it.
1:43 But requests has this cool method called raise_for_status. So if anything went wrong for whatever reason,
1:48 you'll get an error, otherwise you can just keep going. So now we have, and we can print out, what we got back here, and then again.
2:00 And notice, there's all the results coming back, well, at least all the ones the server would give us. That's pretty cool.
2:06 Now, we actually, not to like... We don't want to work with strings, we want to work with data. So the next thing that we need to do
2:14 is convert this text into Python dictionaries from the JSON source.


Talk Python's Mastodon Michael Kennedy's Mastodon