Last active
January 3, 2023 07:00
-
-
Save altwitt/3d0fc4dcfaf8234bcba0d636d4e357f7 to your computer and use it in GitHub Desktop.
Make a vessel-tracking map with python -- https://digital-geography.com/vessel-tracking-the-python-way/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.marinetraffic.com/ | |
#module import | |
import urllib.request | |
from bs4 import BeautifulSoup | |
import re | |
import pandas as pd | |
from datetime import datetime | |
import locale | |
locale.setlocale(locale.LC_ALL, 'en_US') #as we need to deal with names of monthes later on. | |
import os | |
IMOS = [9733612, 9391933, 9312834, 9722302, 9472529, 9288708] | |
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', | |
'Accept-Encoding': 'none', | |
'Accept-Language': 'en-US,en;q=0.8', | |
'Connection': 'keep-alive'} | |
items = [] | |
for IMO in IMOS: | |
url = r'https://www.vesselfinder.com/en/vessels/VOS-TRAVELLER-IMO-' + str(IMO) | |
req = urllib.request.Request(url, None, hdr) | |
with urllib.request.urlopen(req) as response: | |
the_page = response.read() | |
parsed_html = BeautifulSoup(the_page) | |
tables = parsed_html.findAll("table") | |
for table in tables: | |
if table.findParent("table") is None: | |
for row in table.findAll('tr'): | |
aux = row.findAll('td') | |
try: | |
if aux[0].string == "Coordinates": | |
coords = aux[1].string | |
if aux[0].string == "Vessel Name": | |
name = aux[1].string | |
if aux[0].string == "Last report": | |
zeit = datetime.strptime(aux[1].contents[1], ' %b %d, %Y %I:%M %Z') | |
except: | |
print("strange table found") | |
coordsSplit = coords.split("/") | |
def dms2dd(degrees,direction): | |
dd = float(degrees) ; | |
if direction == 'S' or direction == 'W': | |
dd *= -1 | |
return dd | |
def parse_dms(dms): | |
parts = re.split(' ', dms) | |
lat = dms2dd(parts[0], parts[1]) | |
return lat | |
lat = parse_dms(coordsSplit[0]) | |
lng = parse_dms(coordsSplit[1]) | |
items.append((lat, lng, name, zeit)) | |
filename = 'ship_positions.txt' | |
if os.path.exists(filename): | |
append_write = 'a' # append if already exists | |
fw = open(filename,append_write) | |
else: | |
append_write = 'w' # make a new file if not | |
fw = open(filename,append_write) | |
fw.write("lat;lng;name;time\n") | |
for item in items: | |
fw.write("%3.5f;%3.5f;%s;%s\n" % item) | |
fw.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment