Operation Manual

Experiments in Python
108
Notes:
Lesson 3.7: Accessing the web – providing a weather forecast
from urllib.request import urlopen
from x ml.do m import minidom
import time
# extract weather details by grabbing tags from the RSS feed
# 'channel' contains all titles that contain weather heading text
def ExtractWeather( d o c ):
for node in doc.getElementsByTagName('channel'):
for title in node.getElementsByTagName('title'):
print(title.rstChild.data)
results = []
bbc_weather = "http://open.live.bbc.co.uk/weather/feeds/en/"
locations = ["2653941", "2655603", "2633352", "2653822", "2650752"]
forecast = "/3dayforecast.rss"
start = time.time()
for location in locations:
# open the rss feed and parse the result into a web document
# and add the doc to the end of the list
results.append(minidom.parse(urlopen(bbc_weather+location+
f o r e c a s t)))
elapsedTime = (time.time() - start))
for webDoc in resu lt s:
ExtractWeather(webDoc)
print("Elapsed Time: %s" % elapsedTime)
We’re getting to the end of the experiments now, and these will require you to have
a web connection. The first part of this experiment shows you how to get weather
reports from an RSS feed. An RSS feed is like a trimmed-down webpage with
only a standard set of data that can be grabbed and displayed. Whenever you
look at a webpage or an RSS feed, you are really grabbing a list of instructions on
how to draw the page in your web browser or feed reader.
This program is set up to find the three-day forecast for five different locations in
the UK. It grabs the pages into what is known as a “Document” and then it
extracts the information marked as “titles” from that document. These contain the
forecast data on the RSS feed.
The main function for grabbing web data is “urlopen”. A URL is a “uniform
resource locator, which is the internet equivalent of a street address for
anything and everything on the web. A call to “urlopen” will stop the Python
program until it finds what you are looking for and returns a result. This might take
less than a second but it could also take several seconds. This might be a problem,
and it explains why you have to wait for your browser to respond to
some webpages.
This program may take a few seconds before it completes fetching all five reports.
The listing takes a snapshot of the time before calling “urlopen” and again after it
completes so that it can show you how long it took.