User:Eleanorg/Thematic1.1/Get Ship Position 1

From XPUB & Lens-Based wiki
< User:Eleanorg
Revision as of 23:31, 28 November 2011 by Eleanorg (talk | contribs) (Created page with "So I'm learning how to grab live data from the interwebs using python. This is a first go at getting the live lat/long info for the ship that goes between Holland and Harwich, UK...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

So I'm learning how to grab live data from the interwebs using python. This is a first go at getting the live lat/long info for the ship that goes between Holland and Harwich, UK. It just searches for a substring within the page based on length, so tends to print extra or missing characters cos the lat/long numbers are not of a constant length.

Next steps:

  • learn how to use something less brain dead, like xpath, to grab the lat/long numbers. (and return them as floats)
  • use the numbers to control the behaviour of something else
# prints the most up-to-date latitude & longitude position for Stena Brittanica ferry (Harwich/Hoek van Holland)
import urllib2        

def getlat():
    page = urllib2.urlopen("http://www.marinetraffic.com/ais/shipdetails.aspx?mmsi=235080274&header=true")
    text = page.read().decode("utf8")
    index = text.find('color=6')
    startlat = index + 9
    endlat = index + 30
    return text[startlat:endlat]

print "Stena Britannica current position is:"
print getlat()