#!/usr/bin/env python3 import requests import json from bs4 import BeautifulSoup response = requests.get('https://www.kwiktrip.com/Maps-Downloads/Store-List') # HACK HACK HACK soup = BeautifulSoup(response.text, 'html.parser') table = soup.find('table') # there's only one, currently; no identifying ID or anything headers = [th.get_text() for th in table.find('thead').find_all('th')] # turn it into a reasonable dict raw_stores = [] for row in table.find('tbody').find_all('tr'): store = {} for (header, cell) in zip(headers, row.find_all('td')): store[header] = cell.get_text() raw_stores.append(store) print(f"""{len(raw_stores)} locations found""") # turn _that_ into GeoJSON Features stores = [] for store in raw_stores: stores.append({ "type": "Feature", "geometry": { "type": "Point", "coordinates": [float(store['Longitude']), float(store['Latitude'])], # yes, [lon, lat] since it's [x, y] }, "properties": { 'address': store['Address'].title(), 'city': store['City'].title(), 'state': store['State'], 'zip': store['Zip'], 'website': f"https://www.kwiktrip.com/locator/store?id={store['Store Number']}", }, }) geojson = { "type": "FeatureCollection", "features": stores, } with open("./data.geojson", "w") as f: f.write(json.dumps(geojson))