Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Using CSS and Beautiful Soup to find values
0:00 Now that we have our css selectors let's go grab those values in Python.
0:04 So let's start with the city.
0:07 We had used the id of location and then within there, there was a heading H1.
0:12 So what we want to do is go to this soup object here and we'll say find,
0:15 now the options for find there is all kinds of options we can pass here,
0:19 notice at the end there is a **kwargs
0:22 that means this methods accepts keyword arguments,
0:26 these are a little bit tricky to figure out what keywords you can use
0:30 but it's an alternate way of calling functions.
0:32 So you can see up here we are using what are called positional parameters,
0:35 saying html is this, the second parameter is the name of the parser to use.
0:39 But down here we are going to say something different,
0:42 we are going to name the parameters,
0:44 we'll say Id = and then id is going to be location, right,
0:46 so this explicitly naming the variables here lets us have more flexibility
0:50 in the way and the order in which we call the function.
0:53 So this is going to go get that div that contains H1
0:56 and then within there we would like to get the actual H1 itself.
1:01 And then get the text, so we can use what is called a fluent interface
1:04 this find method takes one of these soup objects
1:06 but it also returns another one
1:09 there is sort of a sub element that we can just search within the location
1:12 so we can also say now find,
1:14 and the thing to pass here is actually the node
1:17 so we can just say H1 for the node
1:20 and let's just print this out let's call this location like that
1:25 and we just print the lock.
1:27 Now remember I told you the web was a kind of messy place
1:29 so you are going to see this didn't come out
1:32 quite as nice as you would like but that's the web,
1:35 it looks like we got Santa Ana California, very cool,
1:38 but look at all this other stuff,
1:40 this is actually just the node H1 that contain that header,
1:43 if we want just the text we can actually say .get_text
1:47 if I run this again, 92701 is what I had typed,
1:51 and now there we go, we have Santa Ana California
1:54 and all of this basically from here down this is all what came back
1:59 so we are going to have to do some cleanup on this
2:01 but we are in a good place, where this bit of text is going to work well for us.
2:06 Now let's do the others
2:08 so we can say that the condition is going to be soup.find
2:13 remember this is contained within a div
2:15 which has the id current condition,
2:18 so we can similarly go down here and say say id = like that
2:21 then we can say find and this time we are not going to name the element
2:24 but in fact we have a class wx value,
2:27 now in Python class is a keyword so they use class_ here like this
2:31 and we'll say .get_text.
2:36 And let's print out the condition.
2:43 It's overcast, ok, let's just stop for a minute and see how awesome this is,
2:48 so we went and basically two lines of code came up here
2:53 and we downloaded this weather forecast from the internet using Requests
2:56 and just a handful lines more created a Beautiful Soup
3:01 which parse the HTML and we used this css selector style of fluent interface
3:07 to navigate the dome and get the text
3:10 and what we got is right now in Santa Ana it is overcast.
3:15 That is really cool and this is all possible because the PyPi
3:18 the external packages pip to install them, it's really wonderful.
3:23 So let's go ahead and keep going, finish this up,
3:25 so we've got our current condition and then we need one for the temperature,
3:28 we really need two for the temperature
3:30 so we go down here and say call this temp for temperature,
3:34 and wx value that should work for us,
3:37 and then down here we'll have scale as in the unit of measurement,
3:42 so we are going to have like this, now let's just print those out,
3:45 one way to print the series of strings is to just put them comma separated
3:48 and Python will print them out one after another,
3:51 so let's go back to Portland, so 97201, perfect,
3:55 right now it is overcast, 8.6 C, in Portland, how awesome is that?
4:00 So the last thing to do here is some cleanup
4:03 on all these strings and then return them from this method.
4:06 So we'll get to that next.