Python and Weather.com

Discussion in 'Python' started by Fazer, Aug 13, 2004.

  1. Fazer

    Fazer Guest

    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.
    Fazer, Aug 13, 2004
    #1
    1. Advertising

  2. Fazer

    Paul McGuire Guest

    "Fazer" <> wrote in message
    news:...
    > 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 )

    -----------------------------------
    Output (this morning):
    Current temp at Austin, TX is 66°F (19°C)
    Paul McGuire, Aug 13, 2004
    #2
    1. Advertising

  3. Paul,

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

    --
    Kristian
    Kristian M Zoerhoff, Aug 13, 2004
    #3
  4. Fazer

    Michael Fuhr Guest

    Michael Fuhr, Aug 13, 2004
    #4
  5. (Michael Fuhr) wrote in message news:<411cc8e5$>...
    > (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
    Dan Cardamore, Aug 18, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francis Ardi
    Replies:
    3
    Views:
    427
    Sudsy
    Jul 10, 2003
  2. Francis Ardi
    Replies:
    0
    Views:
    464
    Francis Ardi
    Jul 10, 2003
  3. sandeep patil

    WEATHER PROGRAMMING IN PYTHON

    sandeep patil, May 31, 2007, in forum: Python
    Replies:
    2
    Views:
    283
  4. seancron

    Python weather application

    seancron, Aug 30, 2007, in forum: Python
    Replies:
    2
    Views:
    262
  5. seancron

    Python weather application

    seancron, Aug 30, 2007, in forum: Python
    Replies:
    0
    Views:
    227
    seancron
    Aug 30, 2007
Loading...

Share This Page