Module: Fetching Latitude/Longitude Data from the Web
Credit: Will Ware
Given a list of cities, Example 11-1 fetches their latitudes and longitudes from one web site (http://www.astro.ch, a database used for astrology, of all things) and uses them to dynamically build a URL for another web site (http://pubweb.parc.xerox.com), which, in turn, creates a map highlighting the cities against the outlines of continents. Maybe someday a program will be clever enough to load the latitudes and longitudes as waypoints into your GPS receiver.
The code can be vastly improved in several ways. The main fragility
of the recipe comes from relying on the exact format of the HTML page
returned by the http://www.astro.com
site, particularly in the rather clumsy for x in inf.readlines( ) loop in the findcity
function. If this format ever changes, the recipe will break. You
could change the recipe to use htmllib.HTMLParser
instead, and be a tad more immune to modest format changes. This
helps only a little, however. After all, HTML is meant for human
viewers, not for automated parsing and extraction of information. A
better approach would be to find a site serving similar information
in XML (including, quite possibly, XHTML, the XML/HTML hybrid that
combines the strengths of both of its parents) and parse the
information with Python’s powerful XML tools
(covered in Chapter 12).
However, despite this defect, this recipe still stands as an example of the kind of opportunity already afforded today by existing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access