Python and Weather.com

F

Fazer

Hello,

I made a small python script that gives the current weather conditions
of a city that you give as an argument. Here's the basic function
behind it:

I am really a beginnering when it comes to parsing XML but I just used
split to get the values I wanted. Any ideas how I can use proper XML
parsing techniques?

Also, the weather degree is in celcius but I conver it to fahrenheit
as well. :)

Here's the code:

# Get the weather for the city specified
def get_weather(city):
#city = city.replace(" ", "%20")
w = urllib.urlopen("http://xoap.weather.com/search/search?where=%s" %
city.replace(" ", "%20")).read()
# Check if there were matches
if not city.isalpha() or w.find("loc") < 0:
return "No matches found for city of " + city + "!"

# If so, use the first search result and use it
city = w.split("</loc>", 1)[0].split("<loc")[1].split(">")[1]
# Get location id of the first city
locid = w.split("</loc>", 1)[0].split("<loc")[1].split('"', 2)[1]
# Get weather readings
weather = urllib.urlopen("http://xoap.weather.com/weather/local/%s?cc=*&prod=xoap&par=xxx&key=xxx&unit=m"
%
locid).read()
# Get conditions
reading = weather.split("</t>")[0].split("<t>")[1]
# Get temperature
temp = weather.split("</tmp>")[0].split("<tmp>")[1]
if "N/A" in temp:
return "Error"
else:
temp = int(temp)

return "Weather for %s is %s C / %s F and %s" % (city,temp,(temp *
1.8) + 32,reading)


Hope it helps anyone out there. Please feel free to fix anything I
may have done wrong. Comments greatly appreciated!

Thanks,

Faizan S.
 
P

Paul McGuire

Fazer said:
Hello,

I made a small python script that gives the current weather conditions
of a city that you give as an argument. Here's the basic function
behind it:

I am really a beginnering when it comes to parsing XML but I just used
split to get the values I wanted. Any ideas how I can use proper XML
parsing techniques?

Also, the weather degree is in celcius but I conver it to fahrenheit
as well. :)

Here's the code:

# Get the weather for the city specified
def get_weather(city):
#city = city.replace(" ", "%20")
w = urllib.urlopen("http://xoap.weather.com/search/search?where=%s" %
city.replace(" ", "%20")).read()
# Check if there were matches
if not city.isalpha() or w.find("loc") < 0:
return "No matches found for city of " + city + "!"

# If so, use the first search result and use it
city = w.split("</loc>", 1)[0].split("<loc")[1].split(">")[1]
# Get location id of the first city
locid = w.split("</loc>", 1)[0].split("<loc")[1].split('"', 2)[1]
# Get weather readings
weather = urllib.urlopen("http://xoap.weather.com/weather/local/%s?cc=*&prod=xoap&par=
xxx&key=xxx&unit=m"
%
locid).read()
# Get conditions
reading = weather.split("</t>")[0].split("<t>")[1]
# Get temperature
temp = weather.split("</tmp>")[0].split("<tmp>")[1]
if "N/A" in temp:
return "Error"
else:
temp = int(temp)

return "Weather for %s is %s C / %s F and %s" % (city,temp,(temp *
1.8) + 32,reading)


Hope it helps anyone out there. Please feel free to fix anything I
may have done wrong. Comments greatly appreciated!

Thanks,

Faizan S.

Since you asked about other approaches to parsing, here is a pyparsing
rendition of something very similar. I find it a little easier to follow
(and update later) than code with many splits and index references with
special number offsets.

The pyparsing package includes a few other similar examples, such as one
which extracts a list of NTP servers from NIST's web site.

Download pyparsing at http://pyparsing.sourceforge.net .

-- Paul

----------------------------
# getTemp.py
#
# Demonstration of the pyparsing module, doing a simple pattern match
# from an HTML page retrieved using urllib
#
# Copyright 2004, by Paul McGuire
#
from pyparsing import Word, Literal, nums
import urllib

city = "Austin, TX"

# define pattern to match within the HTML
# temperature is given in a string of the form:
# <br><br>67&deg;F<BR>(19&deg;C)
# we want to locate this string within the page, and extract
# the values 67 and 19
makeInt = lambda s,l,t: int(t[0])
integer = Word(nums).setParseAction( makeInt )
currentTempPattern = \
"<br><br>" + \
integer.setResultsName("F") + "&deg;F<br>(" + \
integer.setResultsName("C") + "&deg;C)"

# get current weather for given zip code
noaaURL = "http://www.srh.noaa.gov/zipcity.php?inputstring=%s" %
urllib.quote(city)
weatherPage = urllib.urlopen( noaaURL )
weatherReport = weatherPage.read()
weatherPage.close()

# now use scanString to return a generator of matching patterns, and
# invoke next() to get the first (and expected to be only) matching string
try:
temps,startloc,endloc = currentTempPattern.scanString(
weatherReport ).next()
except StopIteration:
print "Could not extract temperature data from", noaaURL
else:
print "Current temp at %s is %d\xb0F (%d\xb0C)" % \
( city, temps.F, temps.C )
 
K

Kristian M Zoerhoff

Paul,

That's a pretty slick little script; another great contribution to the
community!
 
D

Dan Cardamore


I wrote CurseTheWeather which has both an ncurses front end, and a
python module that can give you access to the forecast data. The
source is the same (xoap.weather.com).

Check it out at (with screenshots for the ncurses gui):
http://opensource.hld.ca/trac.cgi/wiki/CurseTheWeather

If anyone is interested in writing wx/tk/gtk/windows/cocoa/other/guis
then please let me know. The wx gui has been started.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top