#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
0:01 of our application written here,
0:04 but now we want to actually go,
0:05 when somebody calls this function,
0:07 we want to go and download this result
0:09 from our search service, so, recall over here
0:11 in our movie search service, we put something like this.
0:17 We say api/search/{keyword},
0:21 and then the search will happen.
0:22 So let's go over here and first create the URL.
0:26 And the URL we're going to use f-strings.
0:28 This is a Python 3.6 feature, if you don't have
0:31 Python 3.6 or above, then you're going to need to
0:35 do the older style format.
0:36 So we'll say like this, and we want to replace
0:39 that little part with the variable keyword,
0:41 so in Python 3.6 with these f-strings,
0:44 you can say, {keyword}.
0:47 Notice the autocomplete.
0:49 So, let's begin just by printing out URL,
0:52 and just make sure that we're on the right path.
0:54 So we're going to come over here,
0:56 and we're going to call result...
0:59 Spelling is hard.
1:00 Results equalsapi., notice there's our little thing,
1:03 and let's just, for now, say this is going to be runner.
1:06 Going to search for runner.
1:08 So if we run this,
1:10 that looks pretty good, we can click it and test.
1:13 Okay, looks like we got the maze runner and runner runner,
1:16 that's a lot of running.
1:17 And so this is working.
1:19 Now instead of printing out this, let's actually use it.
1:22 So we'll say response equals requests,
1:25 which we've already installed,
1:27 get, and we'll pass the URL.
1:30 We could go ahead and just work with this.
1:32 We could say response.text,
1:34 unless that failed, unless the, say,
1:36 wireless is down or something.
1:37 So we need to be careful and say,
1:38 I want to make sure there was no error.
1:40 There's a status code, you could check it.
1:42 But requests has this cool method called raise_for_status.
1:45 So if anything went wrong for whatever reason,
1:47 you'll get an error, otherwise you can just keep going.
1:51 So now we have, and we can print out, what we got back
1:56 here, and then again.
1:59 And notice, there's all the results coming back,
2:02 well, at least all the ones the server would give us.
2:04 That's pretty cool.
2:05 Now, we actually, not to like...
2:08 We don't want to work with strings, we want to work with data.
2:11 So the next thing that we need to do
2:13 is convert this text into Python dictionaries
2:17 from the JSON source.