FAQ
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.

Search Discussions

  • Paul McGuire at Aug 13, 2004 at 1:14 pm
    "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&parxxx&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 )

    -----------------------------------
    Output (this morning):
    Current temp at Austin, TX is 66?F (19?C)
  • Kristian M Zoerhoff at Aug 13, 2004 at 1:46 pm
    Paul,

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

    --
    Kristian
  • Michael Fuhr at Aug 13, 2004 at 1:57 pm

    faizans at gmail.com (Fazer) writes:

    I made a small python script that gives the current weather conditions
    of a city that you give as an argument.
    You could also use pymetar.

    http://www.schwarzvogel.de/software-pymetar.shtml
  • Dan Cardamore at Aug 18, 2004 at 2:29 am
    mfuhr at fuhr.org (Michael Fuhr) wrote in message news:<411cc8e5$1_2 at omega.dimensional.com>...
    faizans at gmail.com (Fazer) writes:
    I made a small python script that gives the current weather conditions
    of a city that you give as an argument.
    You could also use pymetar.

    http://www.schwarzvogel.de/software-pymetar.shtml
    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 13, '04 at 3:17a
activeAug 18, '04 at 2:29a
posts5
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase