Python and Weather.com

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Aug 13 09:14:20 EDT 2004


"Fazer" <faizans at gmail.com> wrote in message
news:6491b0ab.0408121917.5103b770 at posting.google.com...
> 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°F<BR>(19°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") + "°F<br>(" + \
    integer.setResultsName("C") + "°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 )

-----------------------------------
Output (this morning):
Current temp at Austin, TX is 66°F (19°C)





More information about the Python-list mailing list