[Tutor] Help with Temperature Program

Tim Wilson wilson@visi.com
Mon, 27 May 2002 09:34:28 -0500


On Sun, May 26, 2002 at 11:15:36PM -0700, Joe Kessel wrote:
> 
> Im very new to programming and python.  I need to write a simple program that extracts the current temperature off weather.com.  If anyone is willing to give me some basic instructions, I would appreciate it.

Hi Joe,

You'll want to use the urllib2 module for this. It allows you to read
data from a URL pretty much like a file on your local system (it will
even read URLs on your local system). I did an assignment like this with
my students this year, but we used the National Weather Service's METAR
system. There are METAR stations at airports all around the country. You
can read more about that assignment at
http://www.isd197.org/sibley/cs/icp/assignments/weather_html

The advantage of the METAR data is that it's easily parsable. The
hardest part of used weather.com's data, I suspect, will be finding and
reading the temperature from a Web page that will probably change
frequently. Here's a sample session that shows how to read a URL:

wilson@copland:~$ python
Python 2.1.2 (#1, Jan 18 2002, 18:05:45)
[GCC 2.95.4  (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> metar =
urllib2.urlopen('ftp://weather.noaa.gov/data/observations/metar/stations/KSGS.TXT')
>>> data = metar.read()
>>> print data
2002/05/27 14:17
KSGS 271417Z AUTO 00000KT 10SM CLR 18/13 A2997 RMK AO2 LTG DSNT
     E

>>>

You can split the resulting string and start parsing.

Good luck.

-Tim

-- 
Tim Wilson      |   Visit Sibley online:   | Check out:
Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
W. St. Paul, MN |                          | http://slashdot.org
wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com