Show forecast using Yahoo! Weather

Note: This snippet uses piksemel module to parse XML files. If you don’t have it, read this article to install it.

First of all, you need the WOEID (Where on Earth Identification) of the city. In order to learn it, go to http://weather.yahoo.com and search for the city. You’ll find the WOEID at the end of the url of the page.

# for Istanbul, TR
woeid = 2344116

Use this id in Yahoo! Weather API url, for w parameter.

weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d" % woeid

If you want the results to be in metric units, add u=c parameter into your query.

weatherUri = "http://weather.yahooapis.com/forecastrss?w=%d&u=c" % woeid

Using urllib, fetch the XML file:

import urllib

xml = urllib.urlopen(weatherUri).read()

We can parse this XML easily using piksemel:

import piksemel

temp = piksemel.parseString(xml).getTag('channel').getTag('item').getTag('yweather:condition').getAttribute('temp')

You can also view XML file and look for other resources such as: humidity, visibility, wind, etc.

How to install piksemel module

piksemel is a easy to use python XML parser, based on iksemel.

Not: If you’re using Pardus, piksemel is preinstalled on your system.

Not: If you’re using Windows, before you continue make sure you’ve already installed mingw32 to your system and its directory is included in PATH.

Steps for Linux users:

  • Download the package from http://cekirdek.pardus.org.tr/~bahadir/piksemel/piksemel-1.3.1.tar.gz and unpack it.
  • Run:
    python setup.py build
  • Run:
    python setup.py install

Steps for Windows users:

  • Download the package from http://cekirdek.pardus.org.tr/~bahadir/piksemel/piksemel-1.3.1.tar.gz and unpack it.
  • In piksemel-1.3.1 directory, run:
    setup.py bdist_wininst build --compiler=mingw32
  • Run the installer file under the piksemel-1.3.1/dist directory.