From: Scott Gasch Date: Wed, 27 Apr 2022 02:36:56 +0000 (-0700) Subject: Cleanup geocode. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=10f3dbfb14c357e70055d76031bd0345abfc1759;p=python_utils.git Cleanup geocode. --- diff --git a/geocode.py b/geocode.py index 1ee7f20..023fe33 100644 --- a/geocode.py +++ b/geocode.py @@ -9,7 +9,7 @@ from typing import Dict, Optional import requests from bs4 import BeautifulSoup -from requests.utils import quote +from requests.utils import requote_uri logger = logging.getLogger(__name__) @@ -22,14 +22,14 @@ def geocode_address(address: str) -> Optional[Dict[str, str]]: '5 SHELBERN DR, LINCROFT, NJ, 07738' """ - encoded_address = quote(address) url = 'https://geocoding.geo.census.gov/geocoder/geographies/onelineaddress' - url += f'?address={encoded_address}' + url += f'?address={address}' url += '&layers=all&benchmark=4&vintage=4' + url = requote_uri(url) logger.debug('GET: %s', url) r = requests.get(url) if r.status_code != 200: - logger.error(f'Unexpected response code {r.status_code}, wanted 200. Fail.') + logger.error('Unexpected response code %d, wanted 200. Fail.', r.status_code) return None else: soup = BeautifulSoup(r.text, 'html.parser') @@ -40,7 +40,8 @@ def geocode_address(address: str) -> Optional[Dict[str, str]]: out = {} for line in output.split('\n'): if re.match(r'.*: *$', line): - label = line[:-2].strip() + line = line.strip() + label = line[:-1] logger.debug('Label is: "%s"', label) else: if label: