Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: What website are we using for weather data anyway?
0:00 The website that we are going to use for our example
0:02 is called Weather Underground.
0:04 Now, Weather Underground is a super cool weather site,
0:08 there is a bunch of weather sites out there,
0:10 national weather service, things like this
0:12 but the Weather Underground is kind of different
0:15 and see, all of these green dots, these are all kind of in my neighborhood
0:18 where I am in Germany right now
0:20 these are all personal weather stations that individuals have put at their house
0:24 and then linked up to weather underground
0:26 and so if you live right here, you can actually get the report from where is that,
0:31 a few blocks down the street so this is really cool.
0:34 Now, if we want the weather in say Portland,
0:36 we can come over here and we can just search for it,
0:39 or we could just go wunderground.com/weather-forecast/zip code.
0:45 So, here you can see the local forecast, see if there is some rain,
0:49 yeah there is some rain out, towards the mountains, the cascade,
0:51 what we are going to do is we are going to go and actually grab this HTML
0:55 and we are going to download it
0:56 and we are going to pull this element out right here and use that for a report,
1:00 we are going to pull out this temperature right here the wind,
1:03 the weather forecast in terms of whether it's rainy or sunny or so on,
1:08 we are going to take all that, we are going to turn that into something that we can display to the user.
1:13 Now, one thing I want to say really quickly
1:15 before we get into this is this concept is called screen scraping,
1:18 and it may or may not be allowed by various websites
1:21 if you are trying to write an app and you want to basically
1:24 turn this web pages into an API, you should do two things,
1:27 you should check the terms of service to see
1:30 if that particular website disallows that
1:32 and you should check to see if they have an API,
1:34 so if you go back here you can go to the bottom,
1:36 you can see that these guys actually have a rest API that returns json or xml,
1:43 so if you are going to consume weather form the Weather Underground for real
1:47 please use their API they have a free version, that sort of thing,
1:49 this is only for demonstrating the sort of pip concepts.
1:54 So for starters, let's grab this url, and head over to PyCharm.
2:00 So the next thing we are going to so is get the HTML from the web
2:02 so let's write the method, def get html from web,
2:09 and we have the url, it's going to be here like so,
2:12 now obviously we don't want to always get the weather in Portland Oregon,
2:15 maybe we want to let the user enter it, just like they did up here,
2:18 so let's pass in the zip code, as a string
2:21 and then we want to put it in here.
2:24 And we could kind of do this but I have always kind of preferred
2:28 the format style so I put a little curly brace, the string format and do it like so.
2:33 The next thing we actually have to do is go and download this,
2:37 but let's just test your url really quick
2:38 and this is not going to do much if we don't call it, do a little reformatting,
2:43 so we are going to do that and we'll pass the code like so.
2:46 So what zip code do you want the weather for, let's say 01010,
2:49 see if the even works, here is the url
2:52 we can click this, Brimfield Massachusetts, beautiful.
2:56 So it's actually quite warm in Massachusetts for this time of year,
3:00 it looks like that's working.
3:02 So the next thing we need to do is instead of printing this out
3:05 we need to actually go and get this HTML off the internet,
3:08 in Python 3 we can use something called url lib to download this
3:12 and this is the latest version in Python 3,
3:14 in Python 2 there is actually a url lib 2
3:17 the difference in name is kind of long story not worth going into
3:20 but this is not a super nice API,
3:22 what we want to use is this thing called Requests,
3:25 so we would like to use Requests to go get the data at the other end of that url,
3:30 but Request is not something that is part of the Python standard library,
3:33 chances are many of you don't have it,
3:35 and we need to actually go to this place called the Python package index,
3:39 use the pip tools and download install
3:40 and then we can use the weather app,
3:43 that is one of the core concepts in this application
3:45 so let's stop for a minute and have a look at it.